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
147 views
in Technique[技术] by (71.8m points)

List in python get single line

I have this list, in console:

{'Composer': 'Leighton Pugh'}
{'Copyright': '2016 James Becker'}
{'Part Of Set': '1'}

made by this code:

metadata = []
for output in process.stdout:
info = {}
line = output.strip().split(":", 1)
info[line[0].strip()] = ":".join(line[1:]).strip()
# info[line[0].strip()] = line[1].strip()
metadata.append(info)

it is list as i have this console output:

<class 'list'>

i use this to print/get data:

print(metadata[0].keys())

but how can i get the value from key the per example "Composer"? instead of [0]

question from:https://stackoverflow.com/questions/65874900/list-in-python-get-single-line

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

1 Reply

0 votes
by (71.8m points)

I solved it by, removing the info part, change the metadata to {}, making it an dicionary:

metadata = {}
for output in process.stdout:
    line = output.strip().split(":", 1)
    metadata[line[0].strip()] = ":".join(line[1:]).strip()

and using:

print(metadata['File Name'])

witch gives the correct output, thank you for the comments.


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

...