用Python检查文件是否为CSV格式
有人能提供一个有效的方法来检查一个文件是否是CSV格式吗?用Python来实现。
5 个回答
1
Python有一个叫做csv模块的东西,你可以用它来解析不同类型的CSV文件。
3
补充一下gotgenes的回答:我发现检查那些不应该出现在csv文件中的不可打印字符也能取得不错的效果。
def is_csv(infile):
try:
with open(infile, newline='') as csvfile:
start = csvfile.read(4096)
# isprintable does not allow newlines, printable does not allow umlauts...
if not all([c in string.printable or c.isprintable() for c in start]):
return False
dialect = csv.Sniffer().sniff(start)
return True
except csv.Error:
# Could not get a csv dialect -> probably not a csv.
return False
39
你可以尝试下面这样的做法,但仅仅因为你从 csv.Sniffer
得到了一个方言(也就是文件格式的某种特征),这并不能保证你手里的CSV文件就是有效的。
csv_fileh = open(somefile, 'rb')
try:
dialect = csv.Sniffer().sniff(csv_fileh.read(1024))
# Perform various checks on the dialect (e.g., lineseparator,
# delimiter) to make sure it's sane
# Don't forget to reset the read position back to the start of
# the file before reading any entries.
csv_fileh.seek(0)
except csv.Error:
# File appears not to be in CSV format; move along