0

For example the if[count][2] reads the DNP column and writes the column to the left of it into Ref1. The if statement doesn't work if the column next to the DNP is multiple varibles like 'R20,R30'.

How do you write an if statement that reads multiple inputs?

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!')
ben
  • 9
  • 1
  • Verbatim copy and paste of your previous question ben [here](https://stackoverflow.com/questions/31484849/if-statement-will-not-accept-two-variables-at-the-same-time?noredirect=1#comment51045085_31484849)! – wflynny Jul 21 '15 at 15:19
  • possible duplicate of [How do I read a text file into a string variable in Python](http://stackoverflow.com/questions/8369219/how-do-i-read-a-text-file-into-a-string-variable-in-python) – raym0nd Jul 21 '15 at 15:44

1 Answers1

0

Look into the "quotechar" argument to csv.reader(). What I suspect is that you're wrapping the field in single quotes, when double quotes are the default. If not, please post the failing data.

user3757614
  • 1,776
  • 12
  • 10
  • Yes that does allow for both R20,R17 to be read. Now, how can I split them up to run separately through the program one at a time through my for count in range(len(mytext)-1,0-1): – ben Jul 21 '15 at 14:54
  • Use .split(",") on the column, then loop through each element. – user3757614 Jul 21 '15 at 17:01
  • I used the .split(",") on the column by during the following in my code: Ref1 = (data[count][0]).split(",") The following is the error obtained:['R20', 'R17'] Traceback (most recent call last): File "C:\Python34\Programs\Random.py", line 64, in if Ref1 in mytext[count]: TypeError: 'in ' requires string as left operand, not list – ben Jul 21 '15 at 17:11
  • Where would you put the .split(",") to ensure the two get split apart and still looped? – ben Jul 21 '15 at 17:12