NickV NickV - 1 year ago 60
Python Question

Write a list of strings in a list that may or may not contain commas to a csv in Python

Apologies if there is a really simple answer to this. After two days of searching I haven't found it.

I am scraping a table from a website and building a list of strings by looping. My code works great until there is a comma in one of the values.

This is how I'm building the list (looping structure omitted, clearly):

record = (name, availability, upc, price)
productList.append(",".join(item or "" for item in record))

This results in:

[u'Product One, In Stock, 999999999999, $99.99', u'Product Two, In Stock, ....]

Which I then write to a CSV with:

import unicodecsv as csv


f = open('data.csv', 'wb')
w = csv.writer(f, delimiter = ",")
w.writerow([x.split(',') for x in productList])

Which works great right up until there is a comma in one of the product names. Not surprisingly, at that point it chops the product name into multiple columns.

I appreciate any help you may render. Thank you.

Answer Source

Stop manually adding and removing commas yourself. That's why the csv/unicodecsv modules exist, because you'll get stuff like quoting wrong.

When building your rows, make them plain sequences (lists or tuples) of the fields, not the whole row as a single string:

productList.append([item or "" for item in record])
# If the or "" is to handle Nones only, module already handles this, so you can simplify:

When writing the rows, they're already in the correct form, so no splitting needed:

with open('data.csv', 'wb') as f
    w = csv.writer(f, delimiter = ",")
    # writerows call is just faster way to do:
    # for row in productList: w.writerow(row)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download