将子进程的输出转换为 csv.reader 对象 [英] Converting output from subprocess to csv.reader object

查看:27
本文介绍了将子进程的输出转换为 csv.reader 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从子进程中获取输出并将其转换为可迭代的 csv.reader 或 csv.DictReader 对象?这是我一直在尝试的代码:

p2 = subprocess.Popen("排序命令...", stdout=subprocess.PIPE)输出 = p2.communicate()[0]编辑 = csv.reader(output, delimiter="\t")

基本上,我正在对一个大型 CSV 文件进行排序,然后我想将它作为 csv.reader 对象放入 Python.

我得到的错误是

<块引用>

错误:迭代器应该返回字符串,而不是整数(您是否以文本模式打开文件?)

有没有办法将此字节流视为 csv.reader 对象,或者我是否以错误的方式思考问题?

解决方案

这是 Python 3 中的一个问题.CSV 模块需要 unicode 输入,而不是字节字符串.除此之外,csv.reader() 需要一个可迭代对象,例如打开的文件或字符串列表.试试这个:

encoding = 'ascii' # 指定CSV数据的编码p2 = subprocess.Popen(['sort', '/tmp/data.csv'], stdout=subprocess.PIPE)输出 = p2.communicate()[0].decode(encoding)编辑 = csv.reader(output.splitlines(), delimiter=",")对于编辑中的行:打印(行)

如果 /tmp/data.csv 包含(我使用逗号作为分隔符):

<前>1、2、3、49、10、11、12A B C D5、6、7、8

那么输出将是:

<前>['1', '2', '3', '4']['5', '6', '7', '8']['9'、'10'、'11'、'12']['A B C D']

Is there a way to take the output from subprocess and turn it into an iterable csv.reader or csv.DictReader object? Here's the code I've been trying:

p2 = subprocess.Popen("sort command...", stdout=subprocess.PIPE)
output = p2.communicate()[0]
edits = csv.reader(output, delimiter="\t")

Basically, I'm sorting a large CSV file, and then I'd like to get it into Python as a csv.reader object.

The error I'm getting is

Error: iterator should return strings, not int (did you open the file in text mode?)

Is there a way to treat this bytestream as a csv.reader object, or am I thinking about things the wrong way?

解决方案

This is a problem in Python 3. The CSV module needs unicode input, not byte strings. In addition to this, csv.reader() needs an iterable such as an open file or a list of strings. Try this:

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)

If /tmp/data.csv contains (I've used commas as the separator):

1,2,3,4
9,10,11,12
a,b,c,d
5,6,7,8

then the output would be:

['1', '2', '3', '4']
['5', '6', '7', '8']
['9', '10', '11', '12']
['a', 'b', 'c', 'd']

这篇关于将子进程的输出转换为 csv.reader 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆