newby question:拆分字符串 - 分隔符 [英] newby question: Splitting a string - separator
问题描述
大家好,
i有一个文本文件,其中包含一个带有名字的字符串。
我想把这个字符串拆分成它的记录放一个列表。
在正常中我会做的事情如下:
Hi all,
i am having a textfile which contains a single string with names.
I want to split this string into its records an put them into a list.
In "normal" cases i would do something like:
#!/ usr / bin / python
inp = open(" file")
data = inp.read( )
names = data.split()
inp.close()
#!/usr/bin/python
inp = open("file")
data = inp.read()
names = data.split()
inp.close()
问题是,名称包含空格和记录是还有
刚刚被空格分隔。我唯一可以依赖的是,
recordseparator总是不止一个空格。
我想到的就是定义拆分分隔符()使用
a正则表达式来表示多个空格。 RegEx for whitespace是\,但
我将用于多于一个? \ s +?
TIA,
Tom
The problem is, that the names contain spaces an the records are also
just seprarated by spaces. The only thing i can rely on, ist that the
recordseparator is always more than a single whitespace.
I thought of something like defining the separator for split() by using
a regex for "more than one whitespace". RegEx for whitespace is \s, but
what would i use for "more than one"? \s+?
TIA,
Tom
推荐答案
Thomas Liesner写道:
Thomas Liesner wrote:
大家好,我有一个文本文件,其中包含一个带有名字的字符串。
我想把这个字符串拆分成它的记录放一下列表。
在正常中我会做的事情如下:
Hi all,
i am having a textfile which contains a single string with names.
I want to split this string into its records an put them into a list.
In "normal" cases i would do something like:
#!/ usr / bin / python
inp = open(" file")
data = inp.read( )
names = data.split()
inp.close()
#!/usr/bin/python
inp = open("file")
data = inp.read()
names = data.split()
inp.close()
问题是,名称包含空格,记录也是
刚刚被空间分隔。我唯一可以依赖的是,
recordseparator总是不只是一个空格。
我想到了使用
定义split()的分隔符之类的东西。 多个空格的正则表达式。 RegEx for whitespace是\ s,但
我将用于多个? \ + +
TIA,
Tom
The problem is, that the names contain spaces an the records are also
just seprarated by spaces. The only thing i can rely on, ist that the
recordseparator is always more than a single whitespace.
I thought of something like defining the separator for split() by using
a regex for "more than one whitespace". RegEx for whitespace is \s, but
what would i use for "more than one"? \s+?
TIA,
Tom
\s +给出一个或多个,你需要\s {2,}两个或更多:
\s+ gives one or more, you need \s{2,} for two or more:
import re
re.split(" \s {2,} ",Guido van Rossum Tim Peters Thomas Liesner)
[''Guido van Rossum'',''Tim Peters'',''Thomas Liesner'']
import re
re.split("\s{2,}","Guido van Rossum Tim Peters Thomas Liesner") [''Guido van Rossum'', ''Tim Peters'', ''Thomas Liesner'']
Michael
Michael
Thomas Liesner写道:
Thomas Liesner wrote:
...
我唯一可以依赖的是,
recordseparator总是不止一个空格。
我想到了类似定义的东西split()的分隔符,用于多个空格的正则表达式。 RegEx for whitespace是\ s,但
我将用于多个? \ s +?
...
The only thing i can rely on, ist that the
recordseparator is always more than a single whitespace.
I thought of something like defining the separator for split() by using
a regex for "more than one whitespace". RegEx for whitespace is \s, but
what would i use for "more than one"? \s+?
对于你的分裂正则表达式,你可以说
" \\\ +"
或
" \s {2,}"
这应该适合你:
YOUR_SPLIT_LIST = re.split(" \s {2,}",YOUR_STRING)
你的,
诺亚
>
嗨Tom,
Hi Tom,
多个空格的正则表达式。 RegEx for whitespace是\ s,但
我将用于多个? \ s +?
a regex for "more than one whitespace". RegEx for whitespace is \s, but
what would i use for "more than one"? \s+?
不止一个,我会用
\\\\ + + />
-Jim
For more than one, I''d use
\s\s+
-Jim
这篇关于newby question:拆分字符串 - 分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!