Python - UnicodeDecodeError:'charmap'编解码器无法解码位置1070中的字节0x9d:字符映射到<undefined>

13

我会帮您将一些文件批量运行正则表达式。

正则表达式的代码:

import re


def DOCtoSTRING(path):

   return text

def valuesHextractor(text):
   STRING =""
   #let's find pacient personal info
   regex = re.compile('CONSULTAŢIE(?s).*EXAMENUL')
   pacientdata=regex.search(text)
   pacientinfo = re.sub('[A-Z]+:',"",pacientdata[0])
   STRING=STRING+pacientinfo.strip("CONSULTAŢIE").strip("EXAMENUL")+" "

   #values
   regex = re.compile('EXAMENUL OFTALMOLOGIC:(?s).*TRATAMENT')
   pacientvalues=regex.search(text)

   #AV OD
   #fc
   regex=re.compile("1.AV.*OD.*?fc[;\.\+\- 0-9]*")
   AVfc=regex.search(pacientvalues[0])
   AVODfc=re.sub("1.AV.*OD.*?fc[;\. 0-9]*?","",AVfc[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVODfc)==None:
       AVODfc="None"
   STRING=STRING+AVODfc+" "
   #cc
   regex=re.compile("1.AV.*OD.*?cc[;\.\+\- 0-9]*")
   AVcc=regex.search(pacientvalues[0])
   AVODcc=re.sub("1.AV.*OD.*?cc[;\. 0-9]*?","",AVcc[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVODcc)==None:
       AVODcc="None"
   STRING=STRING+AVODcc+" "
   #cyl
   regex=re.compile("1.AV.*OD.*?cyl[;\.\+\- 0-9]*")
   AVcyl=regex.search(pacientvalues[0])
   AVODcyl=re.sub("1.AV.*OD.*?cyl[;\. 0-9]*?","",AVcyl[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVODcyl)==None:
       AVODcyl="None"
   STRING=STRING+AVODcyl+" "
   #ax
   regex=re.compile("1.AV.*OD.*?ax[;\.\+\- 0-9]*")
   AVax=regex.search(pacientvalues[0])
   AVODax=re.sub("1.AV.*OD.*?ax[;\. 0-9]*?","",AVax[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVODax)==None:
       AVODax="None"
   STRING=STRING+AVODax+" "

   #AV OS
   #fc
   regex=re.compile("1.AV.*OS.*?fc[;\. 0-9]*")
   AVfc=regex.search(pacientvalues[0])
   AVOSfc=re.sub("1.AV.*OS.*?fc[;\. 0-9]*?","",AVfc[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVOSfc)==None:
       AVOSfc="None"
   STRING=STRING+AVOSfc+" "
   #cc
   regex=re.compile("1.AV.*OS.*?cc[;\.\+\- 0-9]*")
   AVcc=regex.search(pacientvalues[0])
   AVOScc=re.sub("1.AV.*OS.*?cc[;\. 0-9]*?","",AVcc[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVOScc)==None:
       AVOScc="None"
   STRING=STRING+AVOScc+" "
   #cyl
   regex=re.compile("1.AV.*OS.*?cyl[;\.\+\- 0-9]*")
   AVcyl=regex.search(pacientvalues[0])
   AVOScyl=re.sub("1.AV.*OS.*?cyl[;\. 0-9]*?","",AVcyl[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVOScyl)==None:
       AVOScyl="None"
   STRING=STRING+AVOScyl+" "
   #ax
   regex=re.compile("1.AV.*OS.*?ax[;\.\+\- 0-9]*")
   AVax=regex.search(pacientvalues[0])
   AVOSax=re.sub("1.AV.*OS.*?ax[;\. 0-9]*?","",AVax[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVOSax)==None:
       AVOSax="None"
   STRING=STRING+AVOSax+" "


############From here ignore names check only the #name

####### DP
   regex=re.compile("1.AV.*OS.*?DP=[;\.\+\- 0-9]*")
   AVax=regex.search(pacientvalues[0])
   AVOSax=re.sub("1.AV.*OS.*?DP=[;\. 0-9]*?","",AVax[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVOSax)==None:
       AVOSax="None"
   DP=AVOSax

##########

   #RO OD
   #cyl
   regex=re.compile("2.RO.*OD.*?cyl[;\.\+\- 0-9]*")
   AVcyl=regex.search(pacientvalues[0])
   AVODcyl=re.sub("2.RO.*OD.*?cyl[;\. 0-9]*?","",AVcyl[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVODcyl)==None:
       AVODcyl="None"
   STRING=STRING+AVODcyl+" "
   #ax
   regex=re.compile("2.RO.*OD.*?ax[;\.\+\- 0-9]*")
   AVax=regex.search(pacientvalues[0])
   AVODax=re.sub("2.RO.*OD.*?ax[;\. 0-9]*?","",AVax[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVODax)==None:
       AVODax="None"
   STRING=STRING+AVODax+" "


   #RO OS
   #cyl
   regex=re.compile("2.RO.*OS.*?cyl[;\.\+\- 0-9]*")
   AVcyl=regex.search(pacientvalues[0])
   AVOScyl=re.sub("2.RO.*OS.*?cyl[;\. 0-9]*?","",AVcyl[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVOScyl)==None:
       AVOScyl="None"
   STRING=STRING+AVOScyl+" "
   #ax
   regex=re.compile("2.RO.*OS.*?ax[;\.\+\- 0-9]*")
   AVax=regex.search(pacientvalues[0])
   AVOSax=re.sub("2.RO.*OS.*?ax[;\. 0-9]*?","",AVax[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVOSax)==None:
       AVOSax="None"
   STRING=STRING+AVOSax+" "

   #3.PIO OD
   regex=re.compile("3.PIO.*OD=[;\.\+\- 0-9]*")
   AVfc=regex.search(pacientvalues[0])
   AVODfc=re.sub("3.PIO.*?OD=[;\. 0-9]*?","",AVfc[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVODfc)==None:
       AVODfc="None"
   STRING=STRING+AVODfc+" "

   #3.PIO OS
   regex=re.compile("3.PIO.*?OS=[;\. 0-9]*")
   AVfc=regex.search(pacientvalues[0])
   AVOSfc=re.sub("3.PIO.*?OS=[;\. 0-9]*?","",AVfc[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVOSfc)==None:
       AVOSfc="None"
   STRING=STRING+AVOSfc+" "


   #4.FO OD
   regex=re.compile("4.FO(?s).*OD:[;\.\+\- 0-9]*")
   AVfc=regex.search(pacientvalues[0])
   AVODfc=re.sub("4.FO(?s).*?OD:[;\. 0-9]*?","",AVfc[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVODfc)==None:
       AVODfc="None"
   STRING=STRING+AVODfc+" "

   #4.FO OS
   regex=re.compile("4.FO(?s).*?OS:[;\. 0-9]*")
   AVfc=regex.search(pacientvalues[0])
   AVOSfc=re.sub("4.FO(?s).*?OS:[;\. 0-9]*?","",AVfc[0])
   regex=re.compile("[\+\-0-9]+")
   if regex.search(AVOSfc)==None:
       AVOSfc="None"
   STRING=STRING+AVOSfc+" "


################################################## NORMAL FROM HERE


   #remove space between a + - and a number    
   STRING=re.sub("\-( )+","-",STRING)
   STRING=re.sub("\+( )+ ","+",STRING)
   STRING=re.sub("(\-\+)","-+ ",STRING)
   STRING=re.sub("(\+\-)","+- ",STRING)

   #treatment
   regex = re.compile('TRATAMENT:(?s).*')
   treatment = regex.search(text)
   treatment = treatment[0].replace('TRATAMENT:',"")
   STRING=STRING+treatment
   STRING=STRING+DP
   regex=re.compile("( )+")
   STRING=STRING.replace('\n',' ').replace('\t', ' ').replace(';','')
   STRING=re.sub(regex," ",STRING)
   print (STRING)

f=open(input("file PATH: ") ,'r')
text=f.read()
valuesHextractor(text)  
f.close

另外一段我正在运行的代码。
f=open("filenames.txt")
for filename in f:
    filename = filename.strip("\n")
    file=open("C:/Users/User/Desktop/toate/"+filename)
    text=file.read()
    valuesHextractor(text)
    file.close()
f.close()

这些文件是.doc格式的,使用的是Microsoft Word 2003

如果使用记事本打开其中一个文件并尝试另存为,会显示它们的编码方式为ANSI

出现以下错误:

== RESTART: C:/Users/User/AppData/Local/Programs/Python/Python37-32/go.py ==
Traceback (most recent call last):
  File "C:/Users/User/AppData/Local/Programs/Python/Python37-32/go.py", line 4, in <module>
    text=file.read()
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1070: character maps to <undefined>
>>> 

我该如何解决这个问题?
3个回答

16
f=open("filenames.txt")
for filename in f:
    filename = filename.strip("\n")
    file=open("C:/Users/User/Desktop/toate/"+filename, encoding="mbcs") # <-----
    text=file.read()
    valuesHextractor(text)
    file.close()
f.close()
根据此Python文档,Ansi编码等同于mbcs,因此使用encoding="mbcs"打开文件应该可以解决问题。 更新:"UnicodeDecodeError"错误已经暗示了Python尝试使用'utf-8'解码但失败了,因此使用'utf-8'不是一个选项。

你能发布完整的回溯信息吗? - thuyein
非常奇怪。我在我的笔记本电脑上尝试了你的代码,它可以正常工作。你的错误信息说函数“valuesHextractor”不存在。但是根据你的代码,它确实存在。你发布的代码是整个文件还是其中一部分? - thuyein
你在 go.py 中是否导入了 valuesHextractor 模块?能否让我看一下你的 go.py 文件?看起来你没有导入这些函数。你在问题中忘记提到它们位于不同的文件中了。 - thuyein

10

使用“utf8”编码打开文件。

f = open("filenames.txt", encoding="utf8")

5
您可以在 Python 脚本顶部添加以下这行代码。
#!/usr/bin/env python
# -*- coding: utf-8 -*-

请将文件以二进制格式读取,这样应该可以正常工作。 替换此行

f=open(input("file PATH: ") ,'r')

使用

f=open(input("file PATH: ") ,'rb')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接