extract rows and filenames from multiple csv files

后端 未结 5 410
醉酒成梦
醉酒成梦 2021-01-17 02:28

I have multiple csv files with date as filename (20080101.csv to 20111031.csv) in a folder. The csv files have common headers. The csv file looks like this:



        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-17 03:03

    You could read in each file at a time. Read it line by line

    files = ['20080101.csv', '20080102.csv', '20080103.csv'] #...etc
    for f in files:
        file = open(f, 'r')
        for line in file:
            ray = line.split(';')
            if (ray[0].strip() == '1' and ray[1].strip() == '2'):
                fout = open('output.csv', 'a')
                fout.write(ray[0].strip() + ' ; ' + ray[1].strip() + ' ; ' + ray[2].strip() + ' ; ' + f + '\n')
                fout.close()
        file.close()
    

    Tested and works. May need some slight modifications.

提交回复
热议问题