Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析这个字符串的Python方式?_Python_Regex_String_Python 2.7 - Fatal编程技术网

解析这个字符串的Python方式?

解析这个字符串的Python方式?,python,regex,string,python-2.7,Python,Regex,String,Python 2.7,我正在分析这行代码 0386 ; Greek # L& GREEK CAPITAL LETTER ALPHA WITH TONOS 基本上,我需要- point = 0386 script = Greek 我就是这样做的 point = line.split(";")[0].replace(" ","") script = line.split("#")[0].split(";")[1].replace(" ","") 我不相信我所做的是最具蟒蛇风格的

我正在分析这行代码

0386          ; Greek # L&       GREEK CAPITAL LETTER ALPHA WITH TONOS
基本上,我需要-

point = 0386
script = Greek
我就是这样做的

point = line.split(";")[0].replace(" ","")
script = line.split("#")[0].split(";")[1].replace(" ","")

我不相信我所做的是最具蟒蛇风格的方式,有没有更优雅的方式?也许是一个正则表达式一行程序?

这就是我应该怎么做的:

>>> s = "0386          ; Greek # L&       GREEK CAPITAL LETTER ALPHA WITH TONOS"
>>> point = s.split(';')[0].strip()
>>> point
'0386'
>>> script = s.split(';')[1].split('#')[0].strip()
>>> script
'Greek'
请注意,您可以重复使用
s.split(“;”)
。因此,将其保存到
var
可能是一个好主意:

>>> var = s.split(';')
>>> point = var[0].strip()  # Strip gets rid of all the whitespace
>>> point
'0386'
>>> script = var[1].split('#')[0].strip()
>>> script
'Greek'
与未绑定方法一起使用:

使用列表理解:

>>> point, script = [word.strip() for word in line.split('#')[0].split(';')]
>>> point
'0386'
>>> script
'Greek'

如果您想要一个正则表达式一行:

point, script = re.search("^(\d+)\s*;\s*(\S+)\s*.*$",s).groups()

其中
s
是您的字符串,当然您需要
导入re

这看起来非常简洁,虽然我不喜欢使用
map
@GamesBrainiac,但我添加了列表理解版本。@GamesBrainiac为什么不使用
map
?它将如何影响性能?@ComputerFellow LCs通常比
map
s快。这种速度差应该不会有什么影响。列表理解通常比映射更容易理解,因为它们更容易阅读。
(“^(.*)\s+;\s+(.*)\s+.*$”,s)。groups()
适合我。上面没有。@ComputerFellow,你的正则表达式将数字与后面的空格匹配。但如果它对你有用,我很高兴!无论如何,这里的重点是展示如何在一行中使用正则表达式。
>>> code, desc = line[:line.rfind('#')].split(';')
>>> code.strip()
'0386'
>>> desc.strip()
'Greek'
>>> code, desc = line[:line.rfind('#')].split(';')
>>> code.strip()
'0386'
>>> desc.strip()
'Greek'