相关疑难解决方法(0)

<0xEF,0xBB,0xBF>字符显示在文件中.如何删除它们?

我正在压缩JavaScript文件,压缩器抱怨我的文件中有字符.

如何搜索这些字符并将其删除?

unicode file utf-8 utf

85
推荐指数
6
解决办法
7万
查看次数

使用Python中的BOM表读取Unicode文件数据

我正在使用Python阅读一系列源代码文件并遇到unicode BOM错误.这是我的代码:

bytes = min(32, os.path.getsize(filename))
raw = open(filename, 'rb').read(bytes)
result = chardet.detect(raw)
encoding = result['encoding']

infile = open(filename, mode, encoding=encoding)
data = infile.read()
infile.close()

print(data)
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在检测编码chardet,然后在内存中读取文件并尝试打印它.包含BOM的Unicode文件的print语句失败,错误如下:

UnicodeEncodeError:'charmap'编解码器无法对位置0-2中的
字符进行编码:字符映射到<undefined>

我猜它正在尝试使用默认字符集解码BOM并且它失败了.如何从字符串中删除BOM以防止这种情况?

python unicode

38
推荐指数
5
解决办法
4万
查看次数

标签 统计

unicode ×2

file ×1

python ×1

utf ×1

utf-8 ×1