简体   繁体   English

Python 2.7解析数据

[英]Python 2.7 parsing data

I have data that look like this: 我有如下数据:

data = 'somekey:value4thekey&second-key:valu3-can.be?anything&third_k3y:it%can have spaces;too'

In a nice human-readable way it would look like this: 以一种人类易于理解的方式,它看起来像这样:

somekey    : value4thekey
second-key : valu3-can.be?anything
third_k3y  : it%can have spaces;too

How should I parse the data so when I do data['somekey'] I would get >>> value4thekey ? 我应该如何解析数据,以便在执行data['somekey']会得到>>> value4thekey

Note: The & is connecting all of the different items 注意: &所有不同的项目

How am I currently tackling with it 我目前如何处理

Currently, I use this ugly solution: 当前,我使用这个丑陋的解决方案:

all = data.split('&')
for i in all:
    if i.startswith('somekey'):
        print i

This solution is very bad due to multiple obvious limitations. 由于多个明显的限制,该解决方案非常糟糕。 It would be much better if I can somehow parse it into a python tree object. 如果我能以某种方式将其解析为python树对象,那就更好了。

I'd split the string by & to get a list of key-value strings, and then split each such string by : to get key-value pairs. 我用&分割字符串以获取键值字符串列表,然后用:分割每个字符串以获取键值对。 Using dict and list comprehensions actually makes this quite elegant: 实际上,使用dict和list的理解使这很优雅:

result = {k:v for k, v in (part.split(':') for part in data.split('&'))}

You can parse your data directly to a dictionary - split on the item separator & then split again on the key,value separator : : 您可以直接解析你的数据字典-分裂的项目分离&再上键,值分隔再次分裂:

table = {
    key: value for key, value in 
    (item.split(':') for item in data.split('&'))
}

This allows you direct access to elements, eg as table['somekey'] . 这使您可以直接访问元素,例如,作为table['somekey']

If you don't have objects within a value, you can parse it to a dictionary 如果值中没有对象,则可以将其解析为字典

structure = {}

for ele in data.split('&'):
    ele_split = ele.split(':')
    structure[ele_split[0]] = ele_split[1]

You can now use structure to get the values: 现在,您可以使用结构来获取值:

print structure["somekey"]
#returns "value4thekey"

Since the keys have a common format of being in the form of "key":"value". 由于键具有“键”:“值”形式的通用格式。 You can use it as a parameter to split on. 您可以将其用作分割参数。

for i in x.split("&"):
      print(i.split(":"))

This would generate an array of even items where every even index is the key and odd index being the value. 这将生成一个偶数项数组,其中每个偶数索引都是键,奇数索引是值。 Iterate through the array and load it into a dictionary. 遍历数组并将其加载到字典中。 You should be good! 你应该很好!

I'd format data to YAML and parse the YAML 我将数据格式化为YAML并解析YAML

import re
import yaml

data = 'somekey:value4thekey&second-key:valu3-can.be?anything&third_k3y:it%can have spaces;too'
yaml_data = re.sub('[:]', ': ', re.sub('[&]', '\n', data ))
y = yaml.load(yaml_data)
for k in y:
        print "%s : %s" % (k,y[k])

Here's the output: 这是输出:

third_k3y : it%can have spaces;too
somekey : value4thekey
second-key : valu3-can.be?anything

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

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