Skip to content
Advertisement

In excel differentiate delimiters from content characters

I have an excel .csv file which looks like this

JavaScript

The data here is comma seperated so I use delimiter=',', but in some cases as in last line there is comma in text too. In my csv file all strings with comma in it are in quotation mark " ". I have tried to read the file with open()

with open(‘file.csv’,’r’) as f:
    for row in f.read().split(‘n’):

But this way I have to handle commas and empty values. So I am curious which is the correct way of working with damaged csv files? should I use regex expressions or libs like pandas ?

Advertisement

Answer

You should use pythons csv module. its designed for exactly this purpose. To take away the complexity of things like parsing csv data where the deliminator can appear inside fields etc.

here is a simple example. I use the csv modules DictReader this will read the first line of the CSV as key names then map each subsequent row as key values. it automatically takes care of the comma inside the supplier field.

JavaScript

OUTPUT

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement