Python - UnicodeDecodeError : 'charmap' codec can't decode byte 0x9d in position 1070: character maps to <undefined>

标签 python python-3.x

尝试对某些文件批量运行正则表达式。

the regex code:

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

The other code I'm running

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

Getting the following error:

== 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>
>>> 

我该如何解决?

最佳答案

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()

Ansi 编码等于mbcs 根据这个python doc .然后用 encoding="mbcs" 打开文件应该可以解决问题。

更新:错误“UnicodeDecodeError”已经暗示 python 已经尝试使用“utf-8”对其进行解码,但失败了。因此,使用“utf-8”不是一种选择。

关于Python - UnicodeDecodeError : 'charmap' codec can't decode byte 0x9d in position 1070: character maps to <undefined>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53954988/

相关文章:

Python Pandas 多个搜索词

python - Django Crispy Forms 添加 HTML 属性

python - 获取与 DOM 元素关联的事件列表

python-3.x - 如果目录不包含 __init__.py 文件,pylint 会引发错误

python - 升级python包时是否需要停止所有python脚本?

python - 为什么在更改超时变量后,Amazon RDS 上的 MySQL 不断使我的连接超时?

python - 如何在 Django 中创建自定义权限(用户角色)?

python 3 regex - 在字符串中查找所有重叠匹配项的开始和结束索引

python-3.x - 在 PyCharm 中重构字符串连接以格式化

python - 即使找到文件,也出现文件未找到错误