0

I work with a sample of CSV file which is like this :

3256221406917,DESCRIPTION1,"U Bio,  U",food
3256223662106,DESCRIPTION2,"U Bio,  U",food

I want to parse it with comas :

def import_csv(csvfilepath):
data = []
product_file = open(csvfilepath, "r")
reader = csv.reader(product_file, delimiter=',')
for row in reader:
    if row:  # avoid blank lines
        columns = [row[0], row[1], row[2], row[3], row[4]]
        data.append(columns)

return data

However it returns a "list index out of range" issue when running.

I believe that the trouble might come from third and fourth column as there is opening and closing double quotes. But I don't understand why the delimiter = ',' seems not used.

Do you know why ? Thank you for your help !

EDIT :

Thank you all I was simply not sure why "," was read after '"' and if there was a way to change it, but it seems simpler to remove the ' "' before !

4
  • 1
    What do you expect ? The row looks like this: ['3256221406917', 'DESCRIPTION1', 'U Bio, U', 'food'] Commented Dec 10, 2018 at 14:07
  • you must check that all rows have 5 columns. you're trying to get row[4] maybe it doesn't exist. use assert len(rows) == 5, 'len 5 expected %s' % ";".join(row) Commented Dec 10, 2018 at 14:09
  • csv module supports reading fields with comma: stackoverflow.com/questions/8311900/… Commented Dec 10, 2018 at 14:10
  • Thank you for the link MGP ! Commented Dec 10, 2018 at 14:22

3 Answers 3

0

I believe you can use pandas for this:

df = pd.read_csv('your-data.csv')
df_to_list = df.values.tolist()
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know pandas, I should read about it. I was wondering how to parse "," before ' " ' while reading with CSV reader, but it seems that it's simply not how it is working and should remove the ' " ' first. Thank you !
0

Try replacing

columns = [row[0], row[1], row[2], row[3], row[4]] with columns = [row[0], row[1], row[2], row[3]]

As there are only 4 columns in the CSV in your example.

Comments

0

I don't see that you need csvreader for this, and I think that if you want to enforce splitting on ALL commas then I guess you can try this approach:

def import_csv(csvfilepath):
  data = []
  with open(csvfilepath, "r") as product_file:
  for r in productfile:
      row = r.split(",")
      if len(r) == 5: # Vary this to change the sensitivity
          columns = [row[0], row[1], row[2], row[3], row[4]]
          data.append(columns)

  return data

2 Comments

I just tried but with this seems to return only the first number ("3")
Yes I had an error in there you're right. I think I edited it out now @SidGabriel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.