我正在尝试这样做,
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。
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)