user3514648
user3514648

Reputation: 101

Unicode Error when importing a csv file in python?

Hi I am using following code to remove duplicate records from my csv file:

inFile = open('I:\SIT\Monthly\LatestMonthly\source\Network1.csv','r')
outFile = open('I:\SIT\Monthly\LatestMonthly\source\Network2.csv','w')

listLines = []

for line in inFile:

    if line in listLines:
        continue

    else:
        outFile.write(line)
        listLines.append(line)

outFile.close()
inFile.close()

When I run the script I am getting an error:

unicodeescape' codec can't decode bytes in position 35-36: malformed \N character escape.

Why do I get this error?

Upvotes: 1

Views: 1299

Answers (1)

nneonneo
nneonneo

Reputation: 179392

Your error occurs before you even open the file!

You are not escaping the backslashes in your filename

'I:\SIT\Monthly\LatestMonthly\source\Network1.csv'

and thus \N is interpreted as a Unicode escape character (\N inserts a Unicode character by name, e.g. '\N{MUSICAL SYMBOL G CLEF}')

You can try using a raw literal here:

r'I:\SIT\Monthly\LatestMonthly\source\Network1.csv'

The prefix r tells Python to treat all backslashes as literal backslashes. Alternatively, you can remember to always escape your backslashes:

'I:\\SIT\\Monthly\\LatestMonthly\\source\\Network1.csv'

or finally, you can use forward-slashes:

'I:/SIT/Monthly/LatestMonthly/source/Network1.csv'

Upvotes: 2

Related Questions