我有这样的代码:
import sys
cities = 'me, myself, eye'
company_types = 'WHAT, WHO'
with open("result.txt", 'w') as fid:
for city in cities:
for company_type in company_types:
fid.write(company_type.strip() + " in " + city.strip())
fid.write("\r\n")我得到了这个输出:
<built-in method write of _io.TextIOWrapper object at 0x0000019E4C3017D0>密码有什么问题吗?我该怎么解决呢?
发布于 2022-10-07 05:19:43
请将城市和company_types转换为列表、或任何可迭代类型。
cities = ['me', 'myself', 'eye']
company_types = ['WHAT', 'WHO']
with open("/full/path/to/file/result.txt", 'w') as fid:
for city in cities:
for company_type in company_types:
fid.write(company_type.strip() + " in " + city.strip())
fid.write("\r\n")尝试使用完整的路径来确保文件是在您想要的位置创建的,而不是Python当前路径。
发布于 2022-10-07 05:24:48
import sys
#you are defining a string instead of a list, Try this.
cities = ['me', 'myself', 'eye']
company_types = ['WHAT', 'WHO']
with open("result.txt", 'w') as fid:
for city in cities:
for company_type in company_types:
fid.write(company_type.strip() + " in " + city.strip())
fid.write("\r\n")发布于 2022-10-07 05:18:51
密码没问题,我已经查过了。但是请检查您的文件,可能它已经出现并需要设置对它的访问。或者使用完整的路径来文件。例如,如果需要在脚本附近创建新文件,则使用:
os.path.join(os.path.dirname(os.path.realpath(__file__)), "result.txt")https://stackoverflow.com/questions/73982620
复制相似问题