This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
dengzeyi-sequenceshield/代码/sequenceShield/cicddos2019/script/merge.py
2022-11-21 12:08:58 +08:00

30 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pandas as pd
import os
# 要拼接的文件夹及其完整路径,注意不要包含中文
Folder_Path = "/home/dzy/coding/SequenceShield/cicddos2019/label"
SaveFile_Path = "/home/dzy/coding/SequenceShield/cicddos2019/label" # 拼接后要保存的文件路径
SaveFile_Name = "all.csv" # 合并后要保存的文件名
# 修改当前工作目录
os.chdir(Folder_Path)
# 将该文件夹下的所有文件名存入一个列表
file_list = os.listdir()
# 排序
file_list.sort(key=lambda x: int(x[:-4]))
# 读取第一个CSV文件并包含表头
print(file_list[0])
# 编码默认UTF-8若乱码自行更改
df = pd.read_csv(Folder_Path + "/" + file_list[0])
# 将读取的第一个CSV文件写入合并后的文件保存
df.to_csv(SaveFile_Path+'/' + SaveFile_Name, encoding="utf_8", index=False)
# 循环遍历列表中各个CSV文件名并追加到合并后的文件
for i in range(1, len(file_list)):
print(file_list[i])
df = pd.read_csv(Folder_Path + '/' + file_list[i])
df.to_csv(SaveFile_Path+'/' + SaveFile_Name, encoding="utf_8",
index=False, header=False, mode='a+')