[你好,我在Mac上尝试使用Python的csv
模块来解析通过stdin传递的以空格分隔的文件:
printf "2020-01-01 Ben 2\n2020-02-01 Jenny 4\n" | ./tmp.py
当我在结尾处调用working
函数运行代码时,得到了预期的结果:
$ printf "2020-01-01 Ben 2\n2020-02-01 Jenny 4\n" | ./tmp.py
function: working
['2020-01-01', 'Ben', '2']
['2020-02-01', 'Jenny', '4']
当我使用最后调用的not_working
函数运行它时,出现错误:
$ printf "2020-01-01 Ben 2\n2020-02-01 Jenny 4\n" | ./tmp.py
function: not_working
Traceback (most recent call last):
File "./tmp.py", line 36, in <module>
not_working() # if working() was here, it would work
File "./tmp.py", line 27, in not_working
print_csv(args.infile, delimiter=' ')
File "./tmp.py", line 20, in print_csv
reader = csv.reader(infile, delimiter)
_csv.Error: unknown dialect
这是在最后调用not_working
的最小示例
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import csv
import sys
parser = argparse.ArgumentParser()
parser.add_argument(
"infile",
nargs="?",
type=argparse.FileType("r"),
default=sys.stdin,
)
args = parser.parse_args()
def print_csv(infile, delimiter):
reader = csv.reader(infile, delimiter)
for row in reader:
print(row)
def not_working():
print("function: not_working")
with args.infile:
print_csv(args.infile, delimiter=' ')
def working():
print("function: working")
with args.infile:
reader = csv.reader(args.infile, delimiter=" ")
for row in reader:
print(row)
not_working() # if working() was here, it would work
# working()
为什么会这样?为什么我可以在函数外部而不是函数内部解析CSV?
您忘记了在delimiter
中将print_csv
作为关键字参数传递;这表示您实际上是在呼叫csv.reader(infile, dialect=delimiter)
,而''是未知方言(通常仅限于'excel'
,'excel-tab'
或'unix'
。
有关更多详细信息,请参阅csv.reader
方法签名的文档。