python3 unicode,Python 3 Unicode编码错误

本文讲述了如何使用glob.glob从目录获取文件时遇到的UnicodeEncodeError,提出了解决方案,包括设置源代码编码、避免非ASCII字符、使用raw字符串或正常字符串并正确处理路径。重点在于确保编码一致性,以及在打印输出时转换字符编码。
摘要由CSDN通过智能技术生成

I'm using glob.glob to get a list of files from a directory input. When trying to open said files, Python fights me back with this error:

UnicodeEncodeError: 'charmap' codec can't encode character '\xf8' in position 18: character maps to < undefined >

By defining a string variable first, I can do this:

filePath = r"C:\Users\Jørgen\Tables\\"

Is there some way to get the 'r' encoding for a variable?

EDIT:

import glob

di = r"C:\Users\Jørgen\Tables\\"

def main():

fileList = getAllFileURLsInDirectory(di)

print(fileList)

def getAllFileURLsInDirectory(directory):

return glob.glob(directory + '*.xls*')

There is a lot more code, but this problem stops the process.

解决方案

Independently on whether you use the raw string literal or a normal string literal, Python interpreter must know the source code encoding. It seems you use some 8-bit encoding, not the UTF-8. Therefore you have to add the line like

# -*- coding: cp1252 -*-

at the beginning of the file (or using another encoding used for the source files). It need not to be the first line, but it usually is the first or second (the first should contain #!python3 for the script used on Windows).

Anyway, it is usually better not to use non ASCII characters in the file/directory names.

You can also use normal slashes in the path (the same way as in Unix-based systems). Also, have a look at os.path.join when you need to compose the paths.

Updated

The problem is probably not where you search it for. My guess is that the error manifests only when you want to display the resulting list via print. This is usually because the console by default uses non-unicode encoding that is not capable to display the character. Try the chcp command without arguments in your cmd window.

You can modify the print command in your main() function to convert the string representation to the ASCII one that can always be displayed:

print(ascii(fileList))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值