这是Python 3中的一个问题。CSV模块需要unicode输入,而不是字节字符串。除此之外,
csv.reader()
需要一个iterable,例如打开的文件或字符串列表。试试这个:
encoding = 'ascii' # specify the encoding of the CSV data
p2 = subprocess.Popen(['sort', '/tmp/data.csv'], stdout=subprocess.PIPE)
output = p2.communicate()[0].decode(encoding)
edits = csv.reader(output.splitlines(), delimiter=",")
for row in edits:
print(row)
如果
/tmp/data.csv
包含(我使用逗号作为分隔符):
1,2,3,4
9,10,11,12
a,b,c,d
5,6,7,8
那么,输出将是:
['1', '2', '3', '4']
['5', '6', '7', '8']
['9', '10', '11', '12']
['a', 'b', 'c', 'd']