在 Python 中拆分具有未知数量空格的字符串作为分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4309684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split a string with unknown number of spaces as separator in Python
提问by user63503
I need a function similar to string.split(' ') but there might be more than one space, and different number of them between the meaningful characters. Something like that:
我需要一个类似于 string.split(' ') 的函数,但可能有多个空格,并且在有意义的字符之间有不同数量的空格。类似的东西:
s = ' 1234 Q-24 2010-11-29 563 abc a6G47er15 '
ss = s.magicSplit()
print ss
['1234','Q-24','2010-11-29','563','abc','a6G47er15']
Can I somehow use regular expressions to catch those spaces in between?
我可以以某种方式使用正则表达式来捕捉它们之间的空间吗?
Could someone help, please?
有人可以帮忙吗?
采纳答案by aaronasterling
Try
尝试
>>> ' 1234 Q-24 2010-11-29 563 abc a6G47er15'.split()
['1234', 'Q-24', '2010-11-29', '563', 'abc', 'a6G47er15']
Or if you want
或者如果你想要
>>> class MagicString(str):
... magicSplit = str.split
...
>>> s = MagicString(' 1234 Q-24 2010-11-29 563 abc a6G47er15')
>>> s.magicSplit()
['1234', 'Q-24', '2010-11-29', '563', 'abc', 'a6G47er15']
回答by Bill Lynch
s = ' 1234 Q-24 2010-11-29 563 abc a6G47er15 '
ss = s.split()
print ss
['1234','Q-24','2010-11-29','563','abc','a6G47er15']
回答by Danny Sanchez
If you have single spaces amid your data (like an address in one field), here's a solution for when the delimiter has two or more spaces:
如果您的数据中有单个空格(例如一个字段中的地址),那么当分隔符有两个或多个空格时,这里有一个解决方案:
with open("textfile.txt") as f:
content = f.readlines()
for line in content:
# Get all variable-length spaces down to two. Then use two spaces as the delimiter.
while line.replace(" ", " ") != line:
line = line.replace(" ", " ")
# The strip is optional here.
data = line.strip().split(" ")
print(data)
回答by Guy de Carufel
To split lines by multiple spaces while keeping single spaces in strings:
要在字符串中保留单个空格的同时按多个空格拆分行:
with open("textfile.txt") as f:
for line in f:
line = [i.strip() for i in line.split(' ') if i]
print(line)
回答by Muthu Kumar
There are many solutions to this question.
这个问题有很多解决方案。
1.) Using split() is the simplest method
1.) 使用 split() 是最简单的方法
s = ' 1234 Q-24 2010-11-29 563 abc a6G47er15 '
s = s.split()
print(s)
Output >> ['1234','Q-24','2010-11-29','563','abc','a6G47er15']
2.) There is another way to solve this using findall() method, you need to "import re" in the starting of your python file.
2.) 还有另一种使用 findall() 方法解决此问题的方法,您需要在 python 文件的开头“导入 re”。
import re
def MagicString(str):
return re.findall(r'\S+', str)
s = ' 1234 Q-24 2010-11-29 563 abc a6G47er15'
s = MagicString(s)
print(s)
print(MagicString(' he ll o'))
Output >> ['1234','Q-24','2010-11-29','563','abc','a6G47er15']
Output >> ['he','ll','o']
3.) If you want to remove any leading (spaces at the beginning) and trailing (spaces at the end) alone use strip().
3.) 如果要单独删除任何前导(开头的空格)和尾随(结尾的空格),请使用 strip()。
s = ' hello '
output = s.strip()
print(output)
Output >> hello

