简体   繁体   中英

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?

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, eg '\\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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM