如何使用命令行参数读取多个文件并使用pandas保存单个输出

时间:2019-01-21 06:01:25

标签: pandas

我有多个TXT格式的文件,如何使用熊猫中的命令行参数将单个输出的所有值合并到一个文件中

像这样:

python3 file1.txt file2.txt file3.txt

代码:

import pandas as pd
import socket, struct
import os
import glob
import sys

try:
    file = sys.argv[1]
except Exception:
    print("Usage: python3 {} [file]".format(sys.argv[0]))
    sys.exit()

os.chdir('/Users/roc/Desktop/js/projj')
fileList = glob.glob('*.txt')
appended_data = []
for file in fileList:
    pdd = pd.read_csv(file,header=None,sep='|',error_bad_lines=False, warn_bad_lines=False,skiprows=[0],names=['Name','Code','Ipv', 'Ip','Range','Date', 'Category'],low_memory=False)
    df = pdd[pdd['Ipv'].str.contains("ipv4") & pdd['Ip'].str.contains('[0-9]')]
    appended_data.append(df)
appended_data = pd.concat(appended_data)
df = pd.DataFrame(appended_data)
pd.options.mode.chained_assignment = None
def ip2int(ip):
    packedIP = socket.inet_aton(ip)
    return struct.unpack("!L", packedIP)[0]
df['Ip'] = df.Ip.apply(ip2int)
df['Range'] = df.groupby(['Code'])['Range'].transform('sum').fillna(0).astype(int)
k = df[['Ip', 'Range', 'Code']].dropna()
df2 = k.drop_duplicates(subset=['Range'])
result_df =df2.sort_values('Range', ascending=True)    
print(result_df.to_csv("/Users/roc/Desktop/js/projj/delegated2.txt",sep=' ', index=False, header=False))

1 个答案:

答案 0 :(得分:1)

使用以下内容遍历文件夹并将所有文件附加到单个数据框中

import os
import glob
os.chdir('C:\\path_to_folder\\')
Filelist = glob.glob('*.txt')
appended_data = []
for file in FileList:
    pdd = pd.read_csv(file,header=None,sep='|',error_bad_lines=False, warn_bad_lines=False,skiprows=[0],names=['Name','Code','Ipv', 'Ip','Range','Date', 'Category'],low_memory=False)
    df = pdd[pdd['Ipv'].str.contains("ipv4") & pdd['Ip'].str.contains('[0-9]')] 
    appended_data.append(df)
appended_data = pd.concat(appended_data)
df = pd.DataFrame(appended_data)

一旦有了df,它将所有文件中的所有数据组合在一起,请使用下面的代码部分:

pd.options.mode.chained_assignment = None 
def ip2int(ip): 
    packedIP = socket.inet_aton(ip) 
    return struct.unpack("!L", packedIP)[0] 
df['Ip'] = df.Ip.apply(ip2int) df['Range'] = df.groupby(['Code'])['Range'].transform('sum').fillna(0).astype(int) 
k = df[['Ip', 'Range', 'Code']].dropna() 
df2 = k.drop_duplicates(subset=['Range']) 
result_df =df2.sort_values('Range', ascending=True)
result_df.to_csv("/Users/roc/Desktop/output.txt",sep=' ', index=False, header=False)