我有一個(gè)這樣的文本文件: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我希望能夠?qū)⒘斜磙D(zhuǎn)換成這樣的: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 我可以創(chuàng)建一個(gè)類似于上面的列表,但是我遇到了具有多個(gè)元素的實(shí)體的問(wèn)題。例如,這是我可以創(chuàng)建的輸出: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我正在處理非常大的列表,因此我很難簡(jiǎn)單地刪除重復(fù)項(xiàng),而且實(shí)體的名稱也各不相同。這是我的代碼: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()
添加回答
舉報(bào)
0/150
提交
取消