将特定行从一个cvs文件传输到另一个cvs文件

2024-05-14 00:23:59 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我试图将[0,1,2,9,10]行从我指定的“e\u文件”转移到“no\u文件”

当我打印“数据”时,我得到了我想要的确切信息,我只是想知道如何将这些数据传输到相应的CSV文件

谢谢你

e_file = '/Users/massive/Desktop//NO/hour.csv'
no_file = '/Users/massive/Desktop/NO/combined.csv'

with open(e_file,'r') as e_r:

    state_code = input("Enter state code: ")
    county_code = input("Enter county code: ")
    station_number = input("Enter station number: ")

    csv_reader_2 = csv.reader(e_r)

    for row in csv_reader_2:

       if row[0] == str(state_code).zfill(2) and row[1] ==str(county_code).zfill(3) and row[2] == str(station_number).zfill(4):
           data = [row[0],row[1],row[2],row[9],row[10]]
           print(data)

Tags: 文件csvnonumberinputcodereaderfile