我想通过使用Visual Studio 2015和Python 3.4编写一个快速脚本来解析Recuva输出日志文件来自学python。VS2015 + Python 3.4文件IO
Starting Point Promo 2013 - Compilation-HD.mp4 15020953 M:\Videos For Future Use\
注意输入文件在文件名,文件大小和输出路径之间有多个空格。
import os
import re
fileName = os.path.join("C:\\", "Users", "temp", "Downloads", "deleted_files.txt")
pattern = r"""
(.*)
\s{2,}
\d+
\s{2,}
(.*)
"""
regex = re.compile(pattern, re.X)
try:
with open(fileName) as inputFile:
# Loop over lines and extract variables of interest
for line in inputFile:
print(line)
match_obj = regex.match(line)
if match_obj: # pattern matched
name = match_obj.group(1) # group 1 is the first object
directory = match_obj.group(2)
print(name + "=>" + directory)
else:
print("Line not matched: " + line)
inputFile.close()
except (OSError, IOError) as err:
raise IOError("%s: %s" % (fileName, err.strerror))
每次我运行代码,我得到的打印(线)的错误,并在VS2015调试器只显示新行。我是否在我的文件打开和读取操作中丢失了某些内容?善良,这是我的第一个Python脚本!
该错误消息我回来就是YTH-'charmap”编解码器无法编码的字符‘在位置1 \ XFE’:字符映射为
编辑(更新试行): 我接收到相同的错误消息,如果我运行命令行脚本:
File "RecuvaRenamer.py", line 21 in print(line) File "C:\Python34\lib\encodings\cp437.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\xfe' in position 1 character maps to
什么错误?到目前为止,代码没有任何明显的错误。 – jonrsharpe
道歉,忘了添加错误: ÿþ'charmap'编解码器无法在位置1编码字符'\ xfe':字符映射到 –
Sraivyn
也许这个问题可以帮助您http://stackoverflow.com/questions/ 14630288/unicodeencodeerror-charmap-codec -cant-encode-character-maps-to-undefined –