Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
124 views
in Technique[技术] by (71.8m points)

How to access and extract data from structured string or list Python

I'm trying to loop around a structured string or list to extract data but I always end up with Type Error or Value error. I tried to convert it to JSON but with the same results. Here is the string:

I want to extract the corresponding data to the values untagged_vlan, tagged_vlan, port_name and port_number. Is there any way to convert this string into slices so I can loop around them and extract the required data?

Thanks in advance

[[{'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"01-01-140"', 'port_no': '1'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"02-01-142"', 'port_no': '2'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"03-01-144"', 'port_no': '3'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"04-01-146"', 'port_no': '4'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"05-01-148"', 'port_no': '5'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"06-01-150"', 'port_no': '6'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"07-01-151"', 'port_no': '7'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"08-01-152"', 'port_no': '8'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"09-01-153"', 'port_no': '9'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"10-01-155"', 'port_no': '10'}]]
question from:https://stackoverflow.com/questions/65648414/how-to-access-and-extract-data-from-structured-string-or-list-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
data = [[{'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"01-01-140"', 'port_no': '1'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"02-01-142"', 'port_no': '2'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"03-01-144"', 'port_no': '3'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"04-01-146"', 'port_no': '4'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"05-01-148"', 'port_no': '5'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"06-01-150"', 'port_no': '6'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"07-01-151"', 'port_no': '7'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"08-01-152"', 'port_no': '8'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"09-01-153"', 'port_no': '9'}, {'untagged_vlan': '101', 'tagged_vlan': '1000', 'port_name': '"10-01-155"', 'port_no': '10'}]]
    
extracted = [item.values() for item in data[0]]

print(extracted)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...