简体   繁体   English

CSV在Python中合并(多个文件)

[英]CSV Merge in Python (Multiple files)

I've recently started learning Python. 我最近开始学习Python。 I came across a problem and thought if somebody can help me. 我遇到一个问题,想知道是否有人可以帮助我。 I am trying to merge .csv files which are in same directory. 我正在尝试合并同一目录中的.csv文件。 A directory with today's date is created everyday and has one more directory in it for city and city directory has the following files. 每天都会创建具有今天日期的目录,并且该目录中还有一个用于city的目录,city目录包含以下文件。

vkSummary1abc   
vkSummary2gbb   
vkSummary3def   
vkDetail1abc
vkDetail2gbb   
vkDetail3def

I am trying to merge all vkDetail files. 我正在尝试合并所有vkDetail文件。 There can be 3 or more files in the directory. 目录中可以有3个或更多文件。 The script should ask the user first which date and city he is trying to run the script on and from that date directory and city directory all the detail files should be merged. 该脚本应首先询问用户他要在哪个日期和城市运行脚本,并应从该日期目录和城市目录中合并所有详细信息文件。 The header should only be there once and then the data. 标头应仅存在一次,然后是数据。

Once you have the date and city from the user you can use glob.glob() to get all the files in the corresponding directory. 从用户那里获得日期和城市后,就可以使用glob.glob()获取相应目录中的所有文件。 From there just open each file in turn, skip past the header row, and write into the result file. 从那里依次打开每个文件,跳过标题行,然后写入结果文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM