文件迭代不返回文件中的数据

时间:2018-11-13 20:00:08

标签: python csv

下面的“我的脚本”应该遍历此文件夹中以csv结尾的文件。它到达每个文件,但删除所有数据,而不是将“ NA”设置为0。

import csv
import os
import sys 

#file location
folderpath = (r'\\Deturbansci19\cvi2$\ClientServices\CC&A\Current\SSO\BRD Work\Parts Opportunity\Sample data')
#print (folderpath)

for root,dirs,files in os.walk(folderpath, topdown = False):
    for name in files:
        if 'Archive' not in root and name.endswith ('.csv'):
            f = os.path.join(r'\\Deturbansci19\cvi2$\ClientServices\CC&A\Current\SSO\BRD Work\Parts Opportunity\Sample data\\'+name)
            #print (os.path.join(root, name))
            newfile = (r'\\Deturbansci19\cvi2$\ClientServices\CC&A\Current\SSO\BRD Work\Parts Opportunity\Sample data\\'+name)
            with open(f, 'rb') as csvfile, open(newfile,'w',newline='') as newcsvfile:
                csv_reader = csv.reader(csvfile, delimiter = ',')
                csv_writer = csv.writer(newcsvfile, delimiter=',')

                for lines in csv_reader:
                    #Replacing lines
                    replaced = lines[7].replace('NA', '0')
                    lines[7] = replaced
                    replaced = lines[8].replace('NA', '0')
                    lines[8] = replaced
                    replaced = lines[17].replace('NA', '0')
                    lines[17] = replaced
                    replaced = lines[18].replace('NA', '0')
                    lines[18] = replaced
                    replaced = lines[19].replace('NA', '0')
                    lines[19] = replaced
                    replaced = lines[20].replace('NA', '0')
                    lines[20] = replaced
                    replaced = lines[21].replace('NA', '0')
                    lines[21] = replaced
                    replaced = lines[22].replace('NA', '0')
                    lines[22] = replaced
                    replaced = lines[23].replace('NA', '0')
                    lines[23] = replaced
                    replaced = lines[24].replace('NA', '0')
                    lines[24] = replaced
                    replaced = lines[25].replace('NA', '0')
                    lines[25] = replaced
                    replaced = lines[26].replace('NA', '0')
                    lines[26] = replaced
                    replaced = lines[27].replace('NA', '0')
                    lines[27] = replaced
                    replaced = lines[28].replace('NA', '0')
                    lines[28] = replaced
                    replaced = lines[29].replace('NA', '0')
                    lines[29] = replaced
                    replaced = lines[30].replace('NA', '0')
                    lines[30] = replaced
                    replaced = lines[31].replace('NA', '0')
                    lines[31] = replaced
                    replaced = lines[32].replace('NA', '0')
                    lines[32] = replaced
                    replaced = lines[33].replace('NA', '0')
                    lines[33] = replaced
                    replaced = lines[34].replace('NA', '0')
                    lines[34] = replaced
                    replaced = lines[35].replace('NA', '0')
                    lines[35] = replaced
                    replaced = lines[36].replace('NA', '0')
                    lines[36] = replaced
                    replaced = lines[37].replace('NA', '0')
                    lines[37] = replaced
                    replaced = lines[38].replace('NA', '0')
                    lines[38] = replaced
                    replaced = lines[39].replace('NA', '0')
                    lines[39] = replaced
                    replaced = lines[40].replace('NA', '0')
                    lines[40] = replaced
                    replaced = lines[41].replace('NA', '0')
                    lines[41] = replaced
                    replaced = lines[42].replace('NA', '0')
                    lines[42] = replaced
                    replaced = lines[43].replace('NA', '0')
                    lines[43] = replaced
                    replaced = lines[44].replace('NA', '0')
                    lines[44] = replaced
                    replaced = lines[45].replace('NA', '0')
                    lines[45] = replaced
                    replaced = lines[46].replace('NA', '0')
                    lines[46] = replaced
                    replaced = lines[47].replace('NA', '0')
                    lines[47] = replaced
                    replaced = lines[48].replace('NA', '0')
                    lines[48] = replaced
                    replaced = lines[49].replace('NA', '0')
                    lines[49] = replaced
                    replaced = lines[50].replace('NA', '0')
                    lines[50] = replaced
                    replaced = lines[51].replace('NA', '0')
                    lines[51] = replaced
                   #Testing Purposes
                   #print (replaced)
                   #replaced = lines[0:50].replace('NA', '0')
                   #lines[0:50] = replaced
                   csv_writer.writerow(lines)

1 个答案:

答案 0 :(得分:2)

您的fnewfile是相同的路径。因此,在您的with语句中,您要打开一个文件进行读取,然后打开相同的文件进行写入。打开文件进行写入会删除该文件。