将可能包含或不包含逗号的列表中的字符串列表写入 Python 中的 csv

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

如果对此有一个非常简单的答案,我们深表歉意。找了两天没找到

我正在从网站上抓取 table 并通过循环构建字符串列表。在其中一个值中有逗号之前,我的代码运行良好。

这就是我构建列表的方式(显然省略了循环结构):

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

这导致:

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

然后我将其写入 CSV:

import unicodecsv as csv

...

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

在其中一个产品名称中有一个逗号之前,它一直很好用。毫不奇怪,此时它将产品名称分成多列。

感谢您提供的任何帮助。谢谢。

停止自己手动添加和删除逗号。这就是 csv/unicodecsv 模块存在的原因,因为你会得到诸如引用错误之类的东西。

构建行时,使它们成为字段的纯序列(lists 或 tuples),而不是将整行作为单个字符串:

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)

写入行时,它们的格式已经正确,因此无需拆分:

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)

在你的 record 变量中你已经有了一个元组,对吧?

无需将您创建的字符串添加到 productList 以连接该元组中的值,而只需添加元组本身:

record = (name, availability, upc, price)
productList.append(record)

然后,使用unicodecsv编写器的writerow方法直接将元组写入文件中。在 packages's web page 中显示的示例中,它显示了如何编写元组。该包将负责用引号将包含逗号的字符串换行。

import unicodecsv as csv

productList = [
    (u'Product One', u'In Stock', 999999999999, u'.99'),
    (u'Product,Two', u'In Stock', 1234, u'.00'),
    (u'Product Three', u'In Stock', 5678, u'.99'),
]

with open("foo.csv", "wb") as f:
    w = csv.writer(f, encoding='utf-8')
    for product in productList:
        w.writerow(product)

这会产生一个合适的 foo.csv:

$ cat foo.csv
Product One,In Stock,999999999999,.99
"Product,Two",In Stock,1234,.00
Product Three,In Stock,5678,.99

(看看 "Product,Two" 是如何用 " 正确包装的?)