如何解决csv.Sniffer未检测到转义字符
我有多个csv文件,我想自动检测它们的定界符,引号字符,转义字符等。我文件的字符。例如,我有一个带双引号的文件,并用反斜杠转义,Sniffer返回None作为转义符而不是反斜杠。
import csv
from itertools import islice
csvfile = 'sample_file.csv'
with open(csvfile,"r") as f:
# read first 200 rows
data = "".join(list(islice(f,200)))
sniffer = csv.Sniffer()
# read file and get info
dialect = sniffer.sniff(data)
print("delimiter: ",dialect.delimiter)
print("escapechar: ",dialect.escapechar)
print("quoting: ",dialect.quoting)
print("doublequote: ",dialect.doublequote)
print("quotechar: ",dialect.quotechar)
输出:
delimiter:,escapechar: None
quoting: 0
doublequote: True
quotechar: "
我的文件看起来像这样:
"ID","Name","City"
"123","Abby","Martha\"s Vineyard"
"456","Brenda","Ike\"s Point"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。