【发布时间】:2019-12-03 13:51:00
【问题描述】:
我有一个要导入的大文件。该文件由数百万行客户创建的数据组成。因此,一些用户使用了编码无法识别的字符(每 100,000 个字符少于 1 个字符)。
但是,这会导致代码中断,因为它无法识别字符,并给我以下错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\x96' in position 619: character maps to <undefined>
在上述特定情况下,编码无法识别长连字符。
我目前用来读取文件并进行一些转换的代码是:
def conversion(path, source, count):
file = open(path, "w")
iFile = open(source, 'r', encoding="utf-8")
len_text = 1
file.write("[\n")
for line in iFile: # For all the lines in the file
line = line.strip() # Remove newline/whitespace from begin and end of line
line = line.replace('"newDetails":{','')
line = line.replace('},"addrDate"',',"addrDate"')
line = line.replace('},"open24Id"',',"open24Id"')
if len_text != count: # While len_text does not equal line_count
line+= r"," # Add , to end of the line
line+= "\n" # Add \n to end of line
file.write(line) # Write line to file
else:
line += "\n" # Add \n to end of line
file.write(line) # Write line to file
len_text += 1 # Increment len_text by 1
file.write("]") # Write ] to end of file
file.close() # Close file
return
中断发生在file.write(line)。
如何让脚本搜索并将字符 \x96 替换为另一个字符?
【问题讨论】:
-
您可以在翻译它的位上尝试一下,然后循环它以使其继续尝试以便捕获错误,然后在 except 中您可以删除或替换它。跨度>
-
不确定我是否关注。不试一试,如果失败,跳过该行?
-
输出文件使用您的平台默认编码,在您的情况下是一些(Windows?)不支持所有字符的 8 位代码页(但如果您运行它可能是不同的编码另一台机器上的脚本)。为什么不对输出文件也使用像 UTF-8 这样的通用编码?
-
如果您绝对必须使用 8 位编码,您应该明确指定它,并且您可以添加一个错误处理程序来替换不支持的字符,例如。
open(path, 'w', encoding='cp1252', errors='replace'). -
顺便说一句,
'\x96'不是破折号,而是控制字符。看起来您可能已经输入了乱码。
标签: python python-3.x utf-8 character-encoding