我是python的新手,我有一个问题。我想要读取多个csv文件,检查标题,并比较文件的标题。
from tkinter import Tk
from tkinter.filedialog import askopenfilenames
import csv
root = Tk()
files= askopenfilenames(parent=root, title='Choose files')
for i in range(len(files)):
f = open('{}'.format(files[i]))
reader = csv.DictReader(f)
row = next(reader)
这就是我已经拥有的代码。我的问题是,我不知道如何读取文件头和比较它们。
我试过了
row[i] = next(reader)
但这是行不通的。
发布于 2018-06-11 03:27:28
一个简单的Python示例,它读取一堆文件并比较它们的头。
last_header = None
# Loop through the files
for file in files:
with open(file) as f:
# Read the header
header = f.readline()
# If this is the first file, then store the
# header for later comparison
if last_header == None:
last_header = header
# Check that the header match the last header
elif header != last_header:
print("Some information to user")
quit()
# Read the remaining lines
lines = f.readlines()
https://stackoverflow.com/questions/50786915
复制相似问题