我有一个长文件,其行以换行符结尾,而字段则以制表符分隔。使用""引用字段。单引号字段也可以包含换行符,并且 - 作为附加转折 - 可以另外包含引用字符串。
以下是一个说明所有情况的示例:
"FieldA" "FieldB" "FieldC"
"AnotherOne" "May contain
newlines" "FieldC"
"Here is one more row" "FieldB" "FieldC"
"And here is a twist" "Some fields with newlines may contain or end with "quotes and"
continue on next line" "FieldC"
我用这种方式尝试了csv模块:
with open(sys.argv[1], 'rU') as csvfile:
a = csv.reader(csvfile, delimiter='\t', quotechar='"')
for row in a:
print len(row)
...但是这给了我可变的行长度,所以我无法可靠地访问字段。如何从Python中可靠地访问这样的文件中的值?