如何使用Python检查CSV是否具有标头? [英] How to check if a CSV has a header using Python?
本文介绍了如何使用Python检查CSV是否具有标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个CSV文件,我想检查第一行中是否只有字符串(即标题).我试图避免使用任何其他功能,例如pandas等.我想我将使用if语句,例如row [0]是字符串打印,这是CSV,但我真的不知道该怎么做: -S有什么建议吗?
I have a CSV file and I want to check if the first row has only strings in it (ie a header). I'm trying to avoid using any extras like pandas etc. I'm thinking I'll use an if statement like if row[0] is a string print this is a CSV but I don't really know how to do that :-S any suggestions?
推荐答案
Python具有内置的 CSV模块可能会有所帮助.例如
Python has a built in CSV module that could help. E.g.
import csv
with open('example.csv', 'rb') as csvfile:
sniffer = csv.Sniffer()
has_header = sniffer.has_header(csvfile.read(2048))
csvfile.seek(0)
# ...
这篇关于如何使用Python检查CSV是否具有标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文