Python 2.7 分割线无法通过尾随反斜杠分割
Python 2.7 splitlines fails to split over trailing backslash
使用 Python 2.7.9,我尝试采用 DOS 风格的目录列表,并使用内置的 splitlines 函数将其拆分为单独的行。该清单是一个目录,我的三重引号字符串中的一行包含尾随反斜杠。问题是没有拆分以反斜杠结尾的行:
# DOS-style listing of the directory "B:\"
listing = """Directory of B:\
12/15/2014 02:12 PM 1814814 BIRD.LOG
01/01/2000 12:04 AM <DIR> CONFIG
12/15/2014 02:55 PM 35060 ALLIGATOR.LOG
03/15/2013 02:06 PM <DIR> MONKEY
03/15/2013 02:06 PM <DIR> FROG
03/15/2013 02:06 PM <DIR> BADGER
2 File(s) 1849874 bytes
4 Dir(s) 1674739712 bytes free
"""
# BIRD.LOG is combined with prior line ending in a backslash
print "keepends = False"
for line in listing.splitlines(False): print repr(line)
# Setting keepends=True does not help
print "keepends = True"
for line in listing.splitlines(True): print repr(line)
这是输出:
keepends = False
'Directory of B: 12/15/2014 02:12 PM 1814814 BIRD.LOG'
' 01/01/2000 12:04 AM <DIR> CONFIG'
' 12/15/2014 02:55 PM 35060 ALLIGATOR.LOG'
' 03/15/2013 02:06 PM <DIR> MONKEY'
' 03/15/2013 02:06 PM <DIR> FROG'
' 03/15/2013 02:06 PM <DIR> BADGER'
' 2 File(s) 1849874 bytes'
' 4 Dir(s) 1674739712 bytes free'
keepends = True
'Directory of B: 12/15/2014 02:12 PM 1814814 BIRD.LOG\n'
' 01/01/2000 12:04 AM <DIR> CONFIG\n'
' 12/15/2014 02:55 PM 35060 ALLIGATOR.LOG\n'
' 03/15/2013 02:06 PM <DIR> MONKEY\n'
' 03/15/2013 02:06 PM <DIR> FROG\n'
' 03/15/2013 02:06 PM <DIR> BADGER\n'
' 2 File(s) 1849874 bytes\n'
' 4 Dir(s) 1674739712 bytes free\n'
问题不变通过keepends = True
。 Python splitlines documentation does not mention any special handling of backslashes, and neither does the documentation for the universal newlines approach 分割线。
我的代码示例来自单元测试,但在现实世界中,列表将以编程方式检索。我可以想到涉及操纵我的输入列表或其他方法的解决方法,但我想知道为什么根本不需要解决方法。这是一个错误吗?如果有任何建议,我们将不胜感激!
您的单元测试的问题是字符串文字中的 \
字符被 Python 解释为转义字符。尝试将第一行更改为
listing = r"""Directory of B:\
来自Python docs:
String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences.
在从命令输出中获取字符串的实际工作场景中,这应该不是问题。
使用 Python 2.7.9,我尝试采用 DOS 风格的目录列表,并使用内置的 splitlines 函数将其拆分为单独的行。该清单是一个目录,我的三重引号字符串中的一行包含尾随反斜杠。问题是没有拆分以反斜杠结尾的行:
# DOS-style listing of the directory "B:\"
listing = """Directory of B:\
12/15/2014 02:12 PM 1814814 BIRD.LOG
01/01/2000 12:04 AM <DIR> CONFIG
12/15/2014 02:55 PM 35060 ALLIGATOR.LOG
03/15/2013 02:06 PM <DIR> MONKEY
03/15/2013 02:06 PM <DIR> FROG
03/15/2013 02:06 PM <DIR> BADGER
2 File(s) 1849874 bytes
4 Dir(s) 1674739712 bytes free
"""
# BIRD.LOG is combined with prior line ending in a backslash
print "keepends = False"
for line in listing.splitlines(False): print repr(line)
# Setting keepends=True does not help
print "keepends = True"
for line in listing.splitlines(True): print repr(line)
这是输出:
keepends = False
'Directory of B: 12/15/2014 02:12 PM 1814814 BIRD.LOG'
' 01/01/2000 12:04 AM <DIR> CONFIG'
' 12/15/2014 02:55 PM 35060 ALLIGATOR.LOG'
' 03/15/2013 02:06 PM <DIR> MONKEY'
' 03/15/2013 02:06 PM <DIR> FROG'
' 03/15/2013 02:06 PM <DIR> BADGER'
' 2 File(s) 1849874 bytes'
' 4 Dir(s) 1674739712 bytes free'
keepends = True
'Directory of B: 12/15/2014 02:12 PM 1814814 BIRD.LOG\n'
' 01/01/2000 12:04 AM <DIR> CONFIG\n'
' 12/15/2014 02:55 PM 35060 ALLIGATOR.LOG\n'
' 03/15/2013 02:06 PM <DIR> MONKEY\n'
' 03/15/2013 02:06 PM <DIR> FROG\n'
' 03/15/2013 02:06 PM <DIR> BADGER\n'
' 2 File(s) 1849874 bytes\n'
' 4 Dir(s) 1674739712 bytes free\n'
问题不变通过keepends = True
。 Python splitlines documentation does not mention any special handling of backslashes, and neither does the documentation for the universal newlines approach 分割线。
我的代码示例来自单元测试,但在现实世界中,列表将以编程方式检索。我可以想到涉及操纵我的输入列表或其他方法的解决方法,但我想知道为什么根本不需要解决方法。这是一个错误吗?如果有任何建议,我们将不胜感激!
您的单元测试的问题是字符串文字中的 \
字符被 Python 解释为转义字符。尝试将第一行更改为
listing = r"""Directory of B:\
来自Python docs:
String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences.
在从命令输出中获取字符串的实际工作场景中,这应该不是问题。