合并多个txt文件,并从生成的大文件中删除重复项

时间:2019-09-29 20:12:10

标签: python-3.x pandas

我一直在努力使这一工作成功,因为:

要合并的文件很大(每个文件最多20MB); 重复的行位于单独的文件中。这就是为什么我需要从生成的合并文件中删除它;

现在,代码正在运行,但是什么都没有显示,它基本上合并了文件,而不处理重复项。

import os
import io
import pandas as pd


merged_df = pd.DataFrame()
for file in os.listdir(r"C:\Users\username\Desktop\txt"):
    if file.endswith(".txt"):
        file_path = os.path.join(r"C:\Users\username\Desktop\txt", file)
        bytes = open(file_path, 'rb').read()
        merged_df = merged_df.append(pd.read_csv(io.StringIO(
            bytes.decode('latin-1')), sep=";", parse_dates=['Data']))
        SellOutCombined = open('test.txt', 'a')
        SellOutCombined.write(merged_df.to_string())
        SellOutCombined.close()


print(len(merged_df))

感谢您的帮助。

0 个答案:

没有答案