首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python csv比较标头

Python csv比较标头
EN

Stack Overflow用户
提问于 2018-06-11 02:37:47
回答 1查看 804关注 0票数 0

我是python的新手,我有一个问题。我想要读取多个csv文件,检查标题,并比较文件的标题。

代码语言:javascript
复制
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)

这就是我已经拥有的代码。我的问题是,我不知道如何读取文件头和比较它们。

我试过了

代码语言:javascript
复制
    row[i] = next(reader)

但这是行不通的。

EN

回答 1

Stack Overflow用户

发布于 2018-06-11 03:27:28

一个简单的Python示例,它读取一堆文件并比较它们的头。

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50786915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档