[英]Column of words to a list of words in quotation marks with commas separating them
I'm trying to get this column of words: 我正试图获取这一列词:
Suzuki music
Chinese music
Conservatory
Blue grass
Rock n roll
Rhythm
Composition
Contra
Instruments
into this format: 进入这种格式:
"suzuki music", "chinese music", "conservatory music", "blue grass", "rock n roll", "rhythm"...
This is what I've tried: 这就是我尝试过的:
stuff = [
Suzuki music
Chinese music
Conservatory
Blue grass
Rock n roll
Rhythm
Composition
Contra
Instruments
]
for line in stuff:
list.append("'" + line + "',")
But I get this error: 但我得到这个错误:
File "/private/var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup At Startup/artsplus_format_script-393966065.996.py", line 2 stuff = [ ^ IndentationError: unexpected indent logout 文件“/ private / var / folders / jv / 9_sy0bn10mbdft1bk9t14qz40000gn / T /在启动时清理/ artsplus_format_script-393966065.996.py”,第2行东西= [^ IndentationError:意外缩进注销
You're looking for the string.join
function 您正在寻找
string.join
函数
For your specific example the code would look like: 对于您的具体示例,代码如下所示:
', '.join(map(lambda x: '"' + x + '"',stuff))
Using the map
function along with the lambda function effectively puts quotes around every element in your stuff
collection. 使用
map
函数和lambda函数可以有效地在stuff
集合中的每个元素周围添加引号。
Assuming you have this in the input.txt
: 假设你在
input.txt
有这个:
Suzuki music
Chinese music
Conservatory
Blue grass
Rock n roll
Rhythm
Composition
Contra
Instruments
Then this code: 然后这段代码:
with open('input.txt', 'r') as f:
print ", ".join(['"%s"' % row.lower() for row in f.read().splitlines()])
will print you: 会打印给你:
"Suzuki music", "Chinese music", "Conservatory", "Blue grass", "Rock n roll", "Rhythm", "Composition", "Contra", "Instruments"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.