小编Ş.T*_*fan的帖子

如何在python中将列表元素分配给不同的文本名称?

我正在努力解决这个问题.

我有一个这样的列表:

  g = ['hey','man','sup']
Run Code Online (Sandbox Code Playgroud)

我想要3个不同的文本文件,如"hey.txt","man.txt","sup.txt".

我用了;

f = open('hey.txt','w')
sys.stdout = f
....
Run Code Online (Sandbox Code Playgroud)

但有没有办法做到这一点?

for x in range(3):
    f = open('g[x].txt','w')
Run Code Online (Sandbox Code Playgroud)

python direction

1
推荐指数
2
解决办法
85
查看次数

标签 统计

direction ×1

python ×1