[英]Combining Multiple CSV files with Python
I am trying to combine multiple csv files into 1 csv file in a python script.我正在尝试在 python 脚本中将多个 csv 文件合并为 1 个 csv 文件。 I want to skip writing the first 5 lines of each csv file.我想跳过编写每个 csv 文件的前 5 行。 Having some trouble and I am new to Python.遇到了一些麻烦,我是 Python 的新手。 I have tried several examples that I have found but it seems to have trouble with the working directory.我已经尝试了几个我发现的例子,但工作目录似乎有问题。 Here is my latest attempt:这是我最近的尝试:
import pandas as pd
import csv
import glob
import os
path = '//server01/tmp/'
files_in_dir = [f for f in os.listdir(path) if f.endswith('csv')]
count = 0
for filenames in files_in_dir:
df = pd.read_csv(filenames)
if count < 6:
count += 1
continue
df.to_csv('out.csv', mode='a')
Any help would be appreciated.任何帮助,将不胜感激。 Thanks!谢谢!
Try this:尝试这个:
import pandas as pd
import csv
import glob
import os
path = '//server01/tmp/'
files_in_dir = [os.path.join(path,f) for f in os.listdir(path) if f.endswith('csv')]
for filenames in files_in_dir:
df = pd.read_csv(filenames, skiprows = 5)
df.to_csv('out.csv', mode='a')
skiprows : number of lines to skip skiprows : 要跳过的行数
nrows : number of rows of file to read nrows : 要读取的文件行数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.