简体   繁体   English

在python列表中获取带有解析的字符串

[英]get string with parsing in python list

i have list like this 我有这样的清单

["<name:john student male age=23 subject=\computer\sience_{20092973}>",
"<name:Ahn professor female age=61 subject=\computer\math_{20092931}>"]

i want to get student using {20092973},{20092931}. 我想让学生使用{20092973},{20092931}。

so i want to split to list like this 所以我想拆分成这样的列表

my expect result 1 is this (input is {20092973}) 我的预期结果1是这个(输入为{20092973})

"student"

my expect result 2 is this (input is {20092931}) 我的预期结果2是这个(输入为{20092931})

"professor"

i already searching... but i can't find.. sorry.. 我已经在搜索...但是我找不到..对不起..

how can i this? 我怎么能这样?

I don't think you should be doing this in the first place. 我不认为您应该首先这样做。 Unlike your toy example, your real problem doesn't involve a string in some clunky format; 与您的玩具示例不同,您的实际问题并不涉及某些笨拙格式的字符串; it involves a Scapy NetworkInterface object. 它涉及一个Scapy NetworkInterface对象。 Which has attributes that you can just access directly. 其中具有可以直接访问的属性。 You only have to parse it because for some reason you stored its string representation. 您只需要解析它,因为出于某种原因您存储了它的字符串表示形式。 Just don't do that; 只是不要那样做。 store the attributes you actually want when you have them as attributes. 当您将它们作为属性时,存储您实际想要的属性。

The NetworkInterface object isn't described in the documentation (because it's an implementation detail of the Windows-specific code), but you can interactively inspect it like any other class in Python (eg, dir(ni) will show you all the attributes), or just look at the source . 文档中没有描述NetworkInterface对象(因为它是Windows特定代码的实现细节),但是您可以像Python中的任何其他类一样交互式地检查它(例如dir(ni)将向您显示所有属性)或仅查看源代码 The values you want are name and win_name . 所需的值为namewin_name So, instead of print ni , just do something like print '%s,%s' % (ni.name, ni.win_name) . 因此,只需执行print '%s,%s' % (ni.name, ni.win_name)代替print ni Then, parsing the results in some other program will be trivial, instead of a pain in the neck. 然后,在其他程序中解析结果将是微不足道的,而不是使颈部感到痛苦。

Or, better, if you're actually using this in Scapy itself, just make the dict directly out of {ni.win_name: ni.name for ni in nis} . 或者,更好的是,如果您实际上是在Scapy本身中使用它,只需直接从{ni.win_name: ni.name for ni in nis}做出{ni.win_name: ni.name for ni in nis} (Or, if you're running Scapy against Python 2.5 or something, dict((ni.win_name, ni.name) for ni in nis) .) (或者,如果您正在针对Python 2.5等运行Scapy dict((ni.win_name, ni.name) for ni in nis) 。)


But to answer the question as you asked it (maybe you already captured all the data and it's too late to capture new data, so now we're stuck working around your earlier mistake…), there are three steps to this: (1) Figure out how to parse one of these strings into its component parts. 但是要回答您所提出的问题(也许您已经捕获了所有数据,现在捕获新数据为时已晚,所以现在我们只能解决您先前的错误……),为此需要执行三个步骤:(1)弄清楚如何将这些字符串之一解析为其组成部分。 (2) Do that in a loop to build a dict mapping the numbers to the names. (2)循环执行以构建将数字映射到名称的字典。 (3) Just use the dict for your lookups. (3)只需使用dict进行查找即可。

For parsing, I'd use a regular expression. 为了进行解析,我将使用正则表达式。 For example: 例如:

<name:\S+\s(\S+).*?\{(\d+)\}>

正则表达式可视化

Debuggex Demo Debuggex演示

Now, let's build the dict: 现在,让我们构建字典:

r = re.compile(r'<name:\S+\s(\S+).*?\{(\d+)\}>')
matches = (r.match(thing) for thing in things)
d = {match.group(2): match.group(1) for match in matches}

And now: 现在:

>>> d['20092973']
'student'

Code: 码:

def grepRole(role, lines):   
    return [line.split()[1] for line in lines if role in line][0]

l = ["<name:john student male age=23 subject=\computer\sience_{20092973}>",
     "<name:Ahn professor female age=61 subject=\compute\math_{20092931}>"]
print(grepRole("{20092973}", l))
print(grepRole("{20092931}", l))

Output: 输出:

student
professor
current_list = ["<name:john student male age=23 subject=\computer\sience_{20092973}>", "<name:Ahn professor female age=61 subject=\computer\math_{20092931}>"]

def get_identity(code):
    print([row.split(' ')[1] for row in current_list if code in row][0])


get_identity("{20092973}")

regular expression is good ,but for me, a rookie, regular expression is another big problem... 正则表达式很好,但是对我来说,菜鸟,正则表达式是另一个大问题...

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

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