代码不会读取CSV列中的多个变量

时间:2015-07-20 17:37:38

标签: python

例如,if [count] [2]读取DNP列并将其左侧的列写入Ref1。如果DNP旁边的列是“R20,R30”等多个变量,则if语句不起作用。

如何编写读取多个输入的if语句?

import os
import os.path
import csv
directory_exists1 = False;
f_exists1 = False;
while (directory_exists1 == False or f_exists1 == False):
    file_path1 = input('Please Enter The Directory Of The .emn File With Back_Slashes /: ')
    directory1 = os.path.dirname(file_path1)
    file_name1 = os.path.basename(file_path1)
    directory_exists1 = os.path.isdir(directory1)
    if directory_exists1 == False:
        print("Incorrect directory")
    f_exists1 = os.path.isfile(file_name1)
    if f_exists1 == False:
        print ("Incorrect file name")
import shutil
os.chdir(directory1)
shutil.copy(directory1+'/'+file_name1,directory1+'/'+"new"+file_name1)
inputfile = open(file_name1)
newfile = open('new'+file_name1,'w')
mytext = inputfile.readlines()
print("")


directory_exists = False;
f_exists = False;
while (directory_exists == False or f_exists == False):
    file_path = input('Please Enter The Directory Of The csv File With Back_Slashes /: ')
    directory = os.path.dirname(file_path)
    file_name = os.path.basename(file_path)
    directory_exists = os.path.isdir(directory)
    if directory_exists == False:
        print("Incorrect directory")
    f_exists = os.path.isfile(file_name)
    if f_exists == False:
        print ("Incorrect file name")
import shutil
os.chdir(directory)
shutil.copy(directory+'/'+file_name,directory+'/'+"new"+file_name)
inputfile2 = open(file_name)
newnewfile = open('new'+file_name,'w')
csv_inputfile = csv.reader((inputfile2),
                           delimiter=',')
data = list(csv_inputfile)

##i=1
for count in range(len(data)):
##    for count in range(0,len(Ref1)):
##        print (Ref1)
##        Ref1 = Ref1[i:i+count]

    if data[count][2] == 'DNP':
        Ref1 = data[count][0]

        ##Ref1 = Ref1.split(",")
        print (Ref1)
        for count in range(len(mytext)-1,0,-1):
            if Ref1 in mytext[count]:
                del mytext[count]
                del mytext[count]

for lines in mytext:
    newfile.write(lines)
print('ENDED!')

1 个答案:

答案 0 :(得分:0)

查看" quotechar" csv.reader()的参数。我怀疑的是,当双引号是默认值时,你用单引号将字段包装起来。如果没有,请发布失败的数据。