I'm having trouble splitting a csv because some of the fields have a "\n" inside them
i'm using:
file_data = csv_file.read().decode("utf-8")
csv_data = file_data.split("\n")
but the fields look something like
'string 1','string 2',
'string
3'
'string 4',
i would like csv_data[0] to be strings 1 and 2, csv_data[1] to be string 3, and csv_data[2] to be string 4
the way i'm currently using, i get csv_data[0] correctly, but string 3 is split in two indexes since it has a /n inside it's text...
CodePudding user response:
You should use the library csv instead of trying to parse it yourself.
Here a link that can help you
CodePudding user response:
Use a library. Python has the csv
module [Python-doc] to parse csv files. I strongly advise to use a parser since the CSV file format is more complicated than it looks like, for example there is syntax to specify quotes and new lines as content of a string.
You can parse the csv content and for example produce a list of lists with:
import csv
with open('mycsv.csv') as mycsv:
csvreader = csv.reader(mycsv)
data = [tuple(row) for row in csvreader]