Karina1117
Karina1117

Reputation: 69

How to resolve Invalid character error in Python

I'm getting an error message saying "invalid character in identifier" when I run this following code: It points at the second line

"results = open(results_path,'w')".

def remove_headers(file_path,header,results_path):
    results = open(results_path,'w')
    results.write(header)
    with open(file_path) as f:
        for line in f:
            if line == header:
                pass
            else:
                results.write(line) 

Could anyone help?

Upvotes: 1

Views: 1357

Answers (2)

Python wala
Python wala

Reputation: 11

your text might be encoded-

with open("filename.txt", "rb") as f:
    contents = f.read().decode("UTF-8")

Upvotes: 1

Raymond Hettinger
Raymond Hettinger

Reputation: 226181

The usual cause of this problem is some invisible character perhaps caused by an odd keystroke combination. The usual solution is to delete the line in question and then retype it.

If you post the full traceback, we can likely be of more help.

Upvotes: 1

Related Questions