最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python中根据系统换行符分割字符串为列表

Python crifan 530浏览 0评论
折腾:
【记录】调试和优化自动抓包工具的代码
期间,对于从运行命令行返回的字符串,可能是多行
希望对于多行字符串,根据不同系统的换行符不同,而自动处理成多行字符串。
python win mac newline
python  newline
python os newline
How to write native newline character to a file descriptor in Python? – Stack Overflow
Handling \r\n vs \n newlines in python on Mac vs Windows – Stack Overflow
很多都是提到的是open函数中的newline
Built-in Functions — Python 3.8.0 documentation
Newline conversion in Python 3 | Python Conquers The Universe
2. Built-in Functions — Python v3.2 documentation
“newline controls how universal newlines works (it only applies to text mode). It can be None, ”, ‘\n’, ‘\r’, and ‘\r\n’. It works as follows:
* On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in ‘\n’, ‘\r’, or ‘\r\n’, and these are translated into ‘\n’ before being returned to the caller. If it is ”, universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.
* On output, if newline is None, any ‘\n’ characters written are translated to the system default line separator, os.linesep. If newline is ”, no translation takes place. If newline is any of the other legal values, any ‘\n’characters written are translated to the given string.”
看到有个:
https://docs.python.org/release/3.2/library/os.html#os.linesep
“os.linesep
The string used to separate (or, rather, terminate) lines on the current platform. This may be a single character, such as ‘\n’ for POSIX, or multiple characters, for example, ‘\r\n’ for Windows. Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single ‘\n’ instead, on all platforms.”
看来就是我要的:
不同系统换行符,已知是:
Linux:\n
Windows:\r\n
Mac:\r
newline does not work with python 2.7 – Stack Overflow
How to convert CRLF to LF on a Windows machine in Python – Stack Overflow
Line endings in python – Stack Overflow
line breaks – reading file without newlines in Python – Stack Overflow
5. Built-in Types — Python 2.7.17 documentation
”str.splitlines([keepends])
Return a list of the lines in the string, breaking at line boundaries. This method uses the universal newlines approach to splitting lines. Line breaks are not included in the resulting list unless keepends is given and true.
Python recognizes “\r”, “\n”, and “\r\n” as line boundaries for 8-bit strings.“
也是我希望要的效果
利用str.splitlines自动拆分多行
所有现在感觉是:
可以用os.linesep:
multipleLineStr.split(os.linesep)
或用str.splitlines
multipleLineStr.splitlines()
此处,通过测试代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Crifan Li
# Function: Test split mutiple line string int string list


import os


def testSplitLines():
    multipleLineStrWin = 'Win: line 1\r\nline 2\r\nline3'
    multipleLineStrUnix = 'Unix: line 1\nline 2\nline3'
    multipleLineStrMac = 'Mac: line 1\rline 2\rline3'
#     multipleLineStrMac = """line 1
# line 2
# lin3"""
    multipleLineStrList = [multipleLineStrWin, multipleLineStrUnix, multipleLineStrMac]
    for eachMultipleLine in multipleLineStrList:
        lineListBysplitlines = eachMultipleLine.splitlines()
        print("lineListBysplitlines=%s" % lineListBysplitlines)


    for eachMultipleLine in multipleLineStrList:
        lineListByLinesep = eachMultipleLine.split(os.linesep) # current mac has already is: \n
        print("lineListByLinesep=%s" % lineListByLinesep)


if __name__ == "__main__":
    testSplitLines()
在当前Mac中测试的效果是:
lineListBysplitlines=['Win: line 1', 'line 2', 'line3']
lineListBysplitlines=['Unix: line 1', 'line 2', 'line3']
lineListBysplitlines=['Mac: line 1', 'line 2', 'line3']
lineListByLinesep=['Win: line 1\r', 'line 2\r', 'line3']
lineListByLinesep=['Unix: line 1', 'line 2', 'line3']
lineListByLinesep=['Mac: line 1\rline 2\rline3']
macOS Mojave
版本:10.14.6
感觉是对于此处最新的Mac中:
os.linesep= \n
而不是旧macOS的
os.linesep= \r
了。
然后也还是:
someStr.splitlines()
最省心和好用。
【总结】
此处,把包含换行的多行字符串,分割成每行的字符串列表,可以用:
someMultipleLineStr.splitlines()
或者是:
在不同平台下,已知换行符不同:
  • Linux:\n
  • Windows:\r\n
  • Mac:
    • 旧Mac:\r
    • 新Mac:\n
然后用:
someMultipleLineStr.split(os.linesep)
即可。

转载请注明:在路上 » 【已解决】Python中根据系统换行符分割字符串为列表

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.209 seconds, using 23.38MB memory