TypeError:“str”对象在Python中不可调用

2024-10-01 09:38:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我为我的项目目的写了以下程序。在

import glob
import os
path = "/home/madhusudan/1/*.txt"
files = glob.glob(path)

s1 = "<add>\n<doc>\n\t<field name = \"id\">"
s2 = "</field>\n"
s3 = "<field name = \"features\">"
s4 = "</doc>\n</add>"

i = 150
for file in files:
    f = open(file,"r")
    str = f.read()
    file1 = "/home/madhusudan/2/"+os.path.splitext(os.path.basename(file))[0] + ".xml"
    f1 = open(file1,"w")
    content = s1 + str(i) + s2 + s3 + f.read() + s2 + s4 
    f1.write(content)
    i = i + 1

运行此代码时,我收到以下错误:

^{pr2}$

有谁能帮我解决这个问题吗?在


Tags: pathnameimportaddfieldhomedocs3
3条回答

str是处理字符串值的保留名称。 因此可以将其用作局部变量名。
选别的。

str_value = f.read()

您已将文件内容分配给str

str = f.read()

这将屏蔽内置函数。在

不要使用str作为本地名称;请选择其他名称。在

在下一行中,代码用string对象覆盖内置函数str。在

str = f.read()

用不同的名字来防止。在


^{pr2}$

相关问题 更多 >