![](/img/trans.png)
[英]Parsing XML in Python: Typeerror: 'str' object is not callable
[英]Python TSV File Parsing and UTF-8 Output Errors (TypeError: 'str' object is not callable)
我有一個
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()
你沒有看過文件; 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)
inputfile
是一個具有屬性的file
對象,該屬性不是方法。 因此你不能稱之為。 因為我真的不知道你要做什么,所以我無法幫你做得對。 您可能希望通過使用.decode('...')
字符串方法解碼文件的內容( inputenc.read()
以將整個內容讀入str
/ buffer
[取決於您的Python版本])。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.