我从一个函数的输出都是在单独的行上打破的值,我想将它变成一个列表。将文本转换为其行的列表
The Score
Leon the Professional
Iron Man
我希望把它变成像一个列表:
movies= ['The Score', 'Leon the Professional', 'Iron Man']
我怎么能去这样做?
我从一个函数的输出都是在单独的行上打破的值,我想将它变成一个列表。将文本转换为其行的列表
The Score
Leon the Professional
Iron Man
我希望把它变成像一个列表:
movies= ['The Score', 'Leon the Professional', 'Iron Man']
我怎么能去这样做?
假设你的输入是一个字符串。
>>> text = '''The Score
Leon the Professional
Iron Man'''
>>> text.splitlines()
['The Score', 'Leon the Professional', 'Iron Man']
有关splitlines()函数的更多信息。
通过“从功能输出”,你的意思是函数返回一个字符串?或打印到标准输出?或写入文件? – 2012-07-27 14:39:47
@Matthew你的例子需要是有效的Python,否则我们只能猜测你有什么。 – jamylak 2012-07-27 14:40:28
我们是否认为这是字符串'“Score \ nLeon专业\ nIron Man”'?只需使用's.split('\ n')'并阅读文档... – mgilson 2012-07-27 14:40:31