判断csv文件名字是否相同,相同就合并,用代码演示
时间: 2023-02-08 07:45:42 浏览: 117
数学建模拟合与插值.ppt
这是一个 Python 代码示例:
```
import os
import pandas as pd
path = "path/to/csv/files"
files = os.listdir(path)
csv_files = [file for file in files if file.endswith(".csv")]
for i in range(len(csv_files)):
for j in range(i+1, len(csv_files)):
if csv_files[i] == csv_files[j]:
print(f"{csv_files[i]} and {csv_files[j]} have the same name.")
# read in the two files and concatenate them
df1 = pd.read_csv(os.path.join(path, csv_files[i]))
df2 = pd.read_csv(os.path.join(path, csv_files[j]))
df = pd.concat([df1, df2])
# write the concatenated file to disk
df.to_csv(os.path.join(path, csv_files[i]), index=False)
# remove the duplicate file
os.remove(os.path.join(path, csv_files[j]))
```
第一部分获取该文件夹下所有csv文件, 第二部分比对文件名是否相同,如果相同,就读取两个文件并进行合并,最后删除重复文件。
阅读全文