我有一个这样的文本文件:APAC230_WINC230,P1-2,Transline,17002,APACHE,230,17105,WINCHSTR,230,1WINC345_VAIL345,P1-2,Transline,16109,WINCHSTR,345,16105,VAIL,345,1WINC345_VAIL345,P1-2,Transline,16109,WINCHSTR,345,16105,VAIL,345,1a我希望能够将列表转换成这样的:APAC230_WINC230,P1-2Transline,17002,APACHE,230,17105,WINCHSTR,230,1WINC345_VAIL345,P1-2Transline,16109,WINCHSTR,345,16105,VAIL,345,1Transline,16109,WINCHSTR,345,16105,VAIL,345,1a使用 pandas read_CSV 我可以创建一个类似于上面的列表,但是我遇到了具有多个元素的实体的问题。例如,这是我可以创建的输出:APAC230_WINC230,P1-2Transline,17002,APACHE,230,17105,WINCHSTR,230,1WINC345_VAIL345,P1-2Transline,16109,WINCHSTR,345,16105,VAIL,345,1WINC345_VAIL345,P1-2Transline,16109,WINCHSTR,345,16105,VAIL,345,1a我正在处理非常大的列表,因此我很难简单地删除重复项,而且实体的名称也各不相同。这是我的代码:import pandas as pd def cgy(input_file): rows=['cgy','cat_con_evt','type','frombusid','frombus','frombuskv', 'tobusid','tobus','tobuskv','circuitid'] df = pd.read_csv(input_file,names=rows,dtype=object) cgy_file = "" cgy_file = input("Enter output file name:") with open(cgy_file, 'w') as f: for i in range(0,len(df)): print(df.loc[i]['cgy']+","+df.loc[i]['cat_con_evt'], file=f) print(df.loc[i]['type']+","+ df.loc[i]['frombusid']+","+df.loc[i]['frombus']+","+df.loc[i]['frombuskv']+","+ df.loc[i]['tobusid']+","+df.loc[i]['tobus']+","+df.loc[i]['tobuskv']+","+df.loc[i]['circuitid'],file=f)def main(): input_file = "" input_file = input("Enter input file name: ") cgy(input_file)if __name__ == '__main__': main()
添加回答
举报
0/150
提交
取消