通过这个简单的 python 练习不断出错

问题描述

我需要将此列表写入文件输出名称,每个名称都在一个新行上。

我尝试:

names = ["John","Oscar","Jacob"]

file = open("names.txt",'w+')

with file as f:
    f.write("%s\n" % i for i in names)

file.close()

file = open('names.txt','r')

print(file)

file.close()

我得到:

TypeError: write() argument must be str,not generator

我也试过:

names = ["John",'w+')

for i in names:
    file.write(i + '\n')

file.close()

file = open('names.txt','r')

print(file)

file.close()

我得到:

<_io.TextIOWrapper name='names.txt' mode='r' encoding='cp1252'>

我使用 Windows Powershell 和手机上的 SoloLearn 应用程序进行了尝试。我做错了什么?!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)