【发布时间】:2016-11-29 03:30:38
【问题描述】:
我正在尝试将一些字符串写入文件但没有引号。
我正在读取列表并尝试将item[0] 写入输出文件。
我尝试映射并执行item[1:-1],但它们都不起作用。
这是我的代码:
for item in sorted(wrong_blocks):
output.write(str(item[0]))
这会继续写入文件:
"word"
但我只想:
word
不带引号
我检查了这个 Python - how to write strings to file without quotes and spaces?
【问题讨论】:
-
您的代码有语法错误。只是缺少括号,还是有更多内容被截断?
-
现在是正确的
-
你试过
output.write(str(item[0])[1:-1])吗? -
是的,还是写引号
-
output.write(str(item[0]).strip('"'))?