-3

I am reposting after changing a few things with my earlier post. thanks to all who gave suggestions earlier. I still have problems with it.

I have a data file (un-structed messy file) from which I have to scrub specific list of strings (delete strings).

Here is what I am doing but with no result:

infile = r"messy_data_file.txt" 
outfile = r"cleaned_file.txt"  

delete_list = ["firstname1 lastname1","firstname2 lastname2"....,"firstnamen lastnamen"] 

fin = open(infile,"") 
fout = open(outfile,"w+") 

for line in fin:     
    for word in delete_list:         
        line = line.replace(word, "")      

    fout.write(line) 

fin.close() 
fout.close() 

When I execute the file, I get the following error:

NameError: name 'word' is not defined
8
  • 1
    Is your code really un-indented? That doesn't work in python, although I'd expect a different error message. Commented Sep 9, 2011 at 18:32
  • your indentation is pretty strange for a python script, are you sure of it ? (copy/paste the code here and then Ctrl+K to format it as code) Commented Sep 9, 2011 at 18:33
  • You should format your code exactly as it is when you're running it since python is whitespace sensitive and it's difficult to know what you are running with the code you posted. Commented Sep 9, 2011 at 18:34
  • 1
    Tried to fix the indentation, but could you check it? Commented Sep 9, 2011 at 18:34
  • duplicate of Python: Deleting specific strings from file Commented Sep 9, 2011 at 18:34

1 Answer 1

1

I'm unable to replicate your error; the error I get with your code is the empty mode string - either put "r" or delete it, read is the default.

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    fin = open(infile, "")
ValueError: empty mode string

Otherwise, seems fine!

Sign up to request clarification or add additional context in comments.

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.