【问题标题】:Python TSV File Parsing and UTF-8 Output Errors (TypeError: 'str' object is not callable)Python TSV 文件解析和 UTF-8 输出错误(TypeError: 'str' object is not callable)
【发布时间】:2015-02-19 16:10:03
【问题描述】:

我有一个

TypeError: 'str' object is not callable

使用此代码(在outputfile.write(...) 行)。输入文件只是一个带有一些特殊字符的 tsv 文件,但我已经能够使用编解码器库来处理它,我不知道我遇到的这个错误是关于什么的。

import codecs
lines_seen = set() # holds lines already seen
inputfile = codecs.open('C:/Users/Allele&Lethality_Rows.txt',encoding='utf-8', mode='r')
outputfile = codecs.open('C:/Users/Removed_Poor_Partial_With.txt', encoding='utf-8', mode='w')

if not "with" in inputfile or "poor" in inputfile or "partially" in inputfile:
    outputfile.write(inputfile.encoding('utf-8'))     
    outputfile.close()

【问题讨论】:

  • 明确指出错误指的是哪一行,这一点至关重要。
  • 当然,抱歉。此行 outputfile.write(inputfile.encoding('utf-8'))
  • 将该信息添加到您的问题中,以便能够取消对您的投票。下次我会请你自己添加这些内容。

标签: python utf-8 tsv


【解决方案1】:

您没有阅读文件; inputfile 仍然是一个文件对象。在codecs.open() 打开的文件对象上,encoding 属性是一个字符串

你需要读取你的文件数据:

with codecs.open('C:/Users/Allele&Lethality_Rows.txt',encoding='utf-8', mode='r') as infile:
    data = infile.read()

if not u"with" in data or u"poor" in data or u"partially" in data:
    with codecs.open('C:/Users/Removed_Poor_Partial_With.txt', encoding='utf-8', mode='w') as outputfile:
        outputfile.write(data)     

【讨论】:

    【解决方案2】:

    inputfile 是具有属性的file 对象,该属性不是方法。因此你不能调用它。因为我真的不知道你想做什么,所以我无法帮助你做对。可能您想使用.decode('...') 字符串方法解码文件的内容(inputenc.read() 将整个内容读入str/buffer [取决于您的 Python 版本])。

    【讨论】:

      猜你喜欢
      • 2017-01-23
      • 2023-03-18
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 2011-04-26
      • 1970-01-01
      • 2019-10-08
      相关资源
      最近更新 更多