问题:如何将多行字符串分成多行?

我有一个多行字符串文字,我想在每一行上执行一个操作,如下所示:

inputString = """Line 1
Line 2
Line 3"""

我想做以下事情:

for line in inputString:
    doStuff()

I have a multi-line string literal that I want to do an operation on each line, like so:

inputString = """Line 1
Line 2
Line 3"""

I want to do something like the following:

for line in inputString:
    doStuff()

回答 0

inputString.splitlines()

将为您提供每个项目的列表,该splitlines()方法旨在将每一行拆分为一个列表元素。

inputString.splitlines()

Will give you a list with each item, the splitlines() method is designed to split each line into a list element.


回答 1

就像其他人说的:

inputString.split('\n')  # --> ['Line 1', 'Line 2', 'Line 3']

与上面的相同,但是不建议使用字符串模块的功能,应避免使用:

import string
string.split(inputString, '\n')  # --> ['Line 1', 'Line 2', 'Line 3']

另外,如果您希望每行都包含中断顺序(CR,LF,CRLF),请将该splitlines方法与True参数一起使用:

inputString.splitlines(True)  # --> ['Line 1\n', 'Line 2\n', 'Line 3']

Like the others said:

inputString.split('\n')  # --> ['Line 1', 'Line 2', 'Line 3']

This is identical to the above, but the string module’s functions are deprecated and should be avoided:

import string
string.split(inputString, '\n')  # --> ['Line 1', 'Line 2', 'Line 3']

Alternatively, if you want each line to include the break sequence (CR,LF,CRLF), use the splitlines method with a True argument:

inputString.splitlines(True)  # --> ['Line 1\n', 'Line 2\n', 'Line 3']

回答 2

使用str.splitlines()

splitlines()不同于,可以正确处理换行符split("\n")

它也具有@efotinis提到的优点,当使用True参数调用时,可以在拆分结果中选择性地包括换行符。


为什么不应该使用的详细说明split("\n")

\n在Python中,代表Unix换行符(ASCII十进制代码10),独立于运行它的平台。但是,换行表示形式取决于平台。在Windows上,\n是两个字符CRLF(ASCII十进制码13和10,\r\n称为AKA 和),而在任何现代Unix(包括OS X)上,它都是单个字符LF

print,例如,即使您有一个行尾与平台不匹配的字符串也可以正常工作:

>>> print " a \n b \r\n c "
 a 
 b 
 c

但是,在“ \ n”上进行显式拆分将产生与平台有关的行为:

>>> " a \n b \r\n c ".split("\n")
[' a ', ' b \r', ' c ']

即使你使用了os.linesep,它只会根据你的平台上的换行分隔符分开,并会失败,如果你在处理文本创建在其他平台上,或用裸\n

>>> " a \n b \r\n c ".split(os.linesep)
[' a \n b ', ' c ']

splitlines 解决了所有这些问题:

>>> " a \n b \r\n c ".splitlines()
[' a ', ' b ', ' c ']

以文本模式读取文件可以部分缓解换行符表示问题,因为它将Python \n转换为平台的换行符表示形式。但是,文本模式仅在Windows上存在。在Unix系统上,所有文件都以二进制模式打开,因此split('\n')在带有Windows文件的UNIX系统中使用将导致不良行为。同样,使用与其他来源(例如来自套接字)的换行符可能不同的字符串来处理字符串也很常见。

Use str.splitlines().

splitlines() handles newlines properly, unlike split("\n").

It also has the the advantage mentioned by @efotinis of optionally including the newline character in the split result when called with a True argument.


Why you shouldn’t use split("\n"):

\n, in Python, represents a Unix line-break (ASCII decimal code 10), independently from the platform where you run it. However, the linebreak representation is platform-dependent. On Windows, \n is two characters, CR and LF (ASCII decimal codes 13 and 10, AKA \r and \n), while on any modern Unix (including OS X), it’s the single character LF.

print, for example, works correctly even if you have a string with line endings that don’t match your platform:

>>> print " a \n b \r\n c "
 a 
 b 
 c

However, explicitly splitting on “\n”, will yield platform-dependent behaviour:

>>> " a \n b \r\n c ".split("\n")
[' a ', ' b \r', ' c ']

Even if you use os.linesep, it will only split according to the newline separator on your platform, and will fail if you’re processing text created in other platforms, or with a bare \n:

>>> " a \n b \r\n c ".split(os.linesep)
[' a \n b ', ' c ']

splitlines solves all these problems:

>>> " a \n b \r\n c ".splitlines()
[' a ', ' b ', ' c ']

Reading files in text mode partially mitigates the newline representation problem, as it converts Python’s \n into the platform’s newline representation. However, text mode only exists on Windows. On Unix systems, all files are opened in binary mode, so using split('\n') in a UNIX system with a Windows file will lead to undesired behavior. Also, it’s not unusual to process strings with potentially different newlines from other sources, such as from a socket.


回答 3

在这种特殊情况下可能会过大,但另一个选择涉及使用StringIO创建文件状对象

for line in StringIO.StringIO(inputString):
    doStuff()

Might be overkill in this particular case but another option involves using StringIO to create a file-like object

for line in StringIO.StringIO(inputString):
    doStuff()

回答 4

原始帖子要求提供代码,该代码将打印一些行(如果在某些情况下是正确的),则打印下一行。我的实现是这样的:

text = """1 sfasdf
asdfasdf
2 sfasdf
asdfgadfg
1 asfasdf
sdfasdgf
"""

text = text.splitlines()
rows_to_print = {}

for line in range(len(text)):
    if text[line][0] == '1':
        rows_to_print = rows_to_print | {line, line + 1}

rows_to_print = sorted(list(rows_to_print))

for i in rows_to_print:
    print(text[i])

The original post requested for code which prints some rows (if they are true for some condition) plus the following row. My implementation would be this:

text = """1 sfasdf
asdfasdf
2 sfasdf
asdfgadfg
1 asfasdf
sdfasdgf
"""

text = text.splitlines()
rows_to_print = {}

for line in range(len(text)):
    if text[line][0] == '1':
        rows_to_print = rows_to_print | {line, line + 1}

rows_to_print = sorted(list(rows_to_print))

for i in rows_to_print:
    print(text[i])

回答 5

我希望注释的代码文本格式正确,因为我认为@ 1_CR的答案需要更多的修改,并且我想扩大他的答案。无论如何,他使我领会了以下技巧:如果可用,它将使用cStringIO(但请注意:cStringIO和StringIO 不相同,因为您不能将cStringIO子类化。。。它是内置的。但是对于基本操作,语法将是相同的,因此您可以这样做):

try:
    import cStringIO
    StringIO = cStringIO
except ImportError:
    import StringIO

for line in StringIO.StringIO(variable_with_multiline_string):
    pass
print line.strip()

I wish comments had proper code text formatting, because I think @1_CR ‘s answer needs more bumps, and I would like to augment his answer. Anyway, He led me to the following technique; it will use cStringIO if available (BUT NOTE: cStringIO and StringIO are not the same, because you cannot subclass cStringIO… it is a built-in… but for basic operations the syntax will be identical, so you can do this):

try:
    import cStringIO
    StringIO = cStringIO
except ImportError:
    import StringIO

for line in StringIO.StringIO(variable_with_multiline_string):
    pass
print line.strip()

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。