小编Tha*_*kay的帖子

我想用列表中的双引号替换单引号

所以我正在制作一个程序,它接受一个文本文件,将其分解为单词,然后将列表写入一个新的文本文件.

我遇到的问题是我需要列表中的字符串是双引号而不是单引号.

例如

['dog','cat','fish']我想要这个时就明白这个["dog","cat","fish"]

这是我的代码

with open('input.txt') as f:
    file = f.readlines()
nonewline = []
for x in file:
    nonewline.append(x[:-1])
words = []
for x in nonewline:
    words = words + x.split()
textfile = open('output.txt','w')
textfile.write(str(words))
Run Code Online (Sandbox Code Playgroud)

我是python的新手,并没有找到任何关于此的内容.有谁知道如何解决这个问题?

[编辑:我忘了提到我在arduino项目中使用输出,要求列表有双引号.]

python string list python-3.x

8
推荐指数
2
解决办法
3万
查看次数

标签 统计

list ×1

python ×1

python-3.x ×1

string ×1