使用python合并csv文件而不重复标题

Shu*_*han 3 python csv split

我正在尝试这样做,

import glob

interesting_files = glob.glob("/home/tcs/PYTHONMAP/test1/*.csv") 

header_saved = False
with open('/home/tcs/PYTHONMAP/output.csv','wb') as fout:
    for filename in interesting_files:
        with open(filename) as fin:
            header =  next(fin)
            if not header_saved:
                fout.write(header)
                header_saved = True
            for line in fin:
                fout.write(line)
Run Code Online (Sandbox Code Playgroud)

并得到

File "/home/tcs/.config/spyder-py3/temp.py", line 11, in <module>
    fout.write(header)

TypeError: a bytes-like object is required, not 'str'
Run Code Online (Sandbox Code Playgroud)

我对python不太了解,请帮助我还想知道如何将1个大csv拆分为多个具有相同标题的csv。

RHS*_*159 15

使用熊猫:

import pandas as pd

interesting_files = glob.glob("/home/tcs/PYTHONMAP/test1/*.csv") 
df = pd.concat((pd.read_csv(f, header = 0) for f in interesting_files))
df.to_csv("output.csv")
Run Code Online (Sandbox Code Playgroud)

删除重复的行:

import pandas as pd

interesting_files = glob.glob("/home/tcs/PYTHONMAP/test1/*.csv") 
df = pd.concat((pd.read_csv(f, header = 0) for f in interesting_files))
df_deduplicated = df.drop_duplicates()
df_deduplicated.to_csv("output.csv")
Run Code Online (Sandbox Code Playgroud)

这不会在创建数据帧时消除重复项,但在此之后。因此,通过连接所有文件来创建数据帧。然后对它进行重复数据删除。然后可以将最终数据帧保存到 csv。

  • 如果您的数据大小无法装入内存,该怎么办?通过这种方法,“df”的大小可能会大于机器 RAM 能够容纳的大小。 (2认同)

Rah*_*hul 3

import glob
import csv
interesting_files = glob.glob("/home/tcs/PYTHONMAP/test1/*.csv") 

header_saved = False
with open('/home/tcs/PYTHONMAP/output.csv', 'w') as fout:
    writer = csv.writer(fout)
    for filename in interesting_files:
        with open(filename) as fin:
            header =  next(fin)
            if not header_saved:
                writer.writerows(header) # you may need to work here. The writerows require an iterable.
                header_saved = True
            writer.writerows(fin.readlines())
Run Code Online (Sandbox Code Playgroud)