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))
[u'Product One, In Stock, 999999999999, $99.99', u'Product Two, In Stock, ....]
import unicodecsv as csv
f = open('data.csv', 'wb')
w = csv.writer(f, delimiter = ",")
w.writerow([x.split(',') for x in productList])
Stop manually adding and removing commas yourself. That's why the
unicodecsv modules exist, because you'll get stuff like quoting wrong.
When building your rows, make them plain sequences (
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: productList.append(record)
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 = ",") w.writerows(productList) # writerows call is just faster way to do: # for row in productList: w.writerow(row)