简体   繁体   English

在python中提取指定字符之间的字符串

[英]Extracting a string between specified characters in python

I'm a newbie to regular expressions and I have the following string: 我是正则表达式的新手,并且具有以下字符串:

sequence = '["{\"First\":\"Belyuen,NT,0801\",\"Second\":\"Belyuen,NT,0801\"}","{\"First\":\"Larrakeyah,NT,0801\",\"Second\":\"Larrakeyah,NT,0801\"}"]'

I am trying to extract the text Belyuen,NT,0801 and Larrakeyah,NT,0801 in python. 我正在尝试在python中提取文本Belyuen,NT,0801Larrakeyah,NT,0801 I have the following code which is not working: 我有以下代码无法正常工作:

re.search('\:\\"...\\', ''.join(sequence))

Ie I want to get the string between characters :\\ and \\ . 即我想获取字符:\\\\之间的字符串。

Don't use regex for this. 不要为此使用正则表达式。 It appears to be a rather strangely split set of JSON strings. 它似乎是一组相当奇怪的JSON字符串集合。 Join them back together and use the json module to decode it. 将它们重新结合在一起,并使用json模块对其进行解码。

import json
sequence = '[%s]' % ','.join(sequence)
data = json.loads(sequence)
print data[0]['First'], data[0]['Second']

(Note the json module is new in Python2.6 - if you have a lower version, download and install simplejson). (请注意,json模块是Python2.6中的新增功能-如果您的版本较低,请下载并安装simplejson)。

it seems like a proper serialization of the Python dict, you could just do: 看起来像是对python字典的正确序列化,您可以这样做:

>>> sequence = ["{\"First\":\"Belyuen,NT,0801\",\"Second\":\"Belyuen,NT,0801\"}","{\"First\":\"Larrakeyah,NT,0801\",\"Second\":\"Larrakeyah,NT,0801\"}"]
>>> import json
>>> for i in sequence:
    d = json.loads(i)
    print(d['First'])


Belyuen,NT,0801
Larrakeyah,NT,0801

you don't need regex 你不需要正则表达式

>>> sequence = ["{\"First\":\"Belyuen,NT,0801\",\"Second\":\"Belyuen,NT,0801\"}","{\"First\":\"Larrakeyah,NT,0801\",\"Second\":\"Larrakeyah,NT,0801\"}"]
>>> for item in sequence:
...  print eval(item).values()
...
['Belyuen,NT,0801', 'Belyuen,NT,0801']
['Larrakeyah,NT,0801', 'Larrakeyah,NT,0801']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM