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

Python how to save first line of stdout as a variable

I am using an api which searches for tvshows and returns an id, the issue I have is that if I save the search as a variable and print the variable it returns output like this:

for result in show:
a = (result.id)
print(a)

84661
30191
30287
80987
111713
8108
50813
72444
42117
22306
4807

I want to save the first result as a variable that I can use later. Have tried a few different things like os.system("echo " + a + " | head -1") but this returns 4807 not 84661, same for awk and sed. What am I doing wrong? I've tried saving the variable as list, str and array but am unable to grab just the first line, It keeps getting the last one.

question from:https://stackoverflow.com/questions/65948424/python-how-to-save-first-line-of-stdout-as-a-variable

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

1 Reply

0 votes
by (71.8m points)
firstIdvariable = show[0].id  

try that code


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

1.4m articles

1.4m replys

5 comments

57.0k users

...