I want to open a text file in a list using Python.
The structure of my text.txt
file is as follows:
"Sentence 1.", "Sentence 2.",..., "Sentence n."
Each sentence is embedded with punctuation marks and separated by comma mark. I want to print it in a list maintaining same structure.
I want the output to be displayed as:
["Sentence 1.", "Sentence 2.",..., "Sentence n."]
But as of now, I am getting the output in the below mentioned format
['"Sentence 1.", "Sentence 2.",..., "Sentence n."']
How to exclude the extra single inverted punctuation mark? Any help is appreciated.
This is my code:
def read_data():
global new_list
with open("text.txt", 'r', encoding = 'utf-8') as f:
raw_data = f.read()
new_list = [raw_data.strip()]
print(new_list)
read_data()
Your text.txt
file is basically a csv file. Using the csv
module we can do the following:
import csv
with open("text.txt", 'r', encoding = 'utf-8') as f:
reader = csv.reader(f, skipinitialspace=True)
new_list = next(reader)