1

I have a variable (aTerm) which contains an 8 digit random number which im using to uniquely identify each individual line of a csv file.

Here is the code I wrote to remove the line which has the corresponding number:

def rebuildFile(aTerm, aFile):
    with open(aFile, 'r') as oldFile, open('static\\new.csv', 'w') as newFile:
        for line in oldFile:
            if not aTerm in line:
                newFile.write(line)
return

The only trouble is that it doesn't work at all unless I type out the 8 digit number as a string in the if statement. So this works perfectly:

def rebuildFile(aTerm, aFile):
    with open(aFile, 'r') as oldFile, open('static\\new.csv', 'w') as newFile:
        for line in oldFile:
            if not "45893243" in line:
                newFile.write(line)
return

I don't understand and would greatly appreciate the help thanks

2 Answers 2

1

You need to cast aTerm to str in order to compare it to a str:

def rebuildFile(aTerm, aFile):
    sTerm = str(aTerm)
    with open(aFile, 'r') as oldFile, open('static\\new.csv', 'w') as newFile:
        for line in oldFile:
            if not sTerm in line:
                newFile.write(line)
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah that did it, I wrote it off because I tried casting it as a string before it went into the function and also in the if statement line and neither worked, but in this exact way it worked first time. Many thanks :)
Hold on actually it didn't work, please excuse me it's nearly 5am casting it to a string doesn't help and I believe it already is a string anyway
0

The problem was that for some reason the innerHTML I had passed from my webpage to flask had a tonne of spaces around it so I had to strip it. This question has been answered by Reblochon Masque.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.