【问题标题】:VS2015 + Python 3.4 File IOVS2015 + Python 3.4 文件IO
【发布时间】:2015-11-03 15:26:21
【问题描述】:

我正在尝试通过使用 Visual Studio 2015 和 Python 3.4 编写一个快速脚本来解析 Recuva 输出日志文件,从而自学 Python。

起点宣传片 2013 - 编译-HD.mp4 15020953 M:\供未来使用的视频\

注意输入文件的文件名、文件大小和输出路径之间有多个空格。

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

每次我运行代码时,我都会在 print(line) 上得到一个错误,并且 VS2015 中的调试器只显示换行符。我在文件打开和读取操作中遗漏了什么吗?善良,这是我的第一个 python 脚本!

我得到的错误信息是 ÿþ-'charmap' codec can't encode character '\xfe' in position 1: character maps to

编辑(更新试用版): 如果我从命令行运行脚本,我会收到相同的错误消息:

文件“RecuvaRenamer.py”,第 21 行 打印(行) 文件“C:\Python34\lib\encodings\cp437.py”,第 19 行,在编码中 返回 codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' 编解码器无法将位置 1 字符映射中的字符 '\xfe' 编码到

【问题讨论】:

  • 什么错误?到目前为止,代码没有任何明显错误。
  • 抱歉,忘记添加错误:ÿþ-'charmap' codec can't encode character '\xfe' in position 1: character maps to
  • 也许这个问题可以帮到你stackoverflow.com/questions/14630288/…
  • @KristianDamian 这是基本问题。谢谢!

标签: python python-3.x visual-studio-2015


【解决方案1】:

好吧,没关系。代码没有错。输入文件未以 UTF-8 编码。将输入转换为 UTF-8 格式,文件“几乎”解析正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 2017-05-03
    • 2021-01-22
    • 2015-03-25
    相关资源
    最近更新 更多