The python data type

File content:
test01 98
test02 87

#!/usr/bin/env python


import subprocess

cat = subprocess.Popen(['cat', 'file'],stdout=subprocess.PIPE,)
grep = subprocess.Popen(['grep', 'test01'],stdin=cat.stdout,stdout=subprocess.PIPE,)
awk = subprocess.Popen(['awk', '{print $2}'],stdin=grep.stdout,stdout=subprocess.PIPE,)
end_of_pipe = awk.stdout
for line in end_of_pipe:
value = line.strip()
print type(value)
print value

The result is as follows:
<type 'str'>
98

Excuse me, how can get plastic 98? For the master.

Started by Harlan at February 11, 2016 - 2:07 AM

Visual inspection of the

value = line.strip()

Instead of

value = int(line.strip())

You can

-----
In addition, say a digression, your code is ugly, cat, grep, awk out

Posted by Natasha at February 25, 2016 - 3:00 AM

Thank you for reply. How can the optimization?

Posted by Harlan at March 06, 2016 - 3:13 AM

I think the use of python, you should use the python method to deal with the problem, always call the shell command, it feels a little neither fish nor fowl, is? It is still not as good as the direct use of shell processing.
  1. #!/usr/bin/env python

  2. for r in open('file').readlines():
  3. v = r.split()
  4. print type(int(v[1])),v[1]

Posted by Natasha at March 14, 2016 - 4:03 AM

Thanks for your reply. Also very much agree with your point of view.

Also want to ask a question, it is these values, I want to assign values to two variables, should be how to write it?
Thank you

Posted by Harlan at March 28, 2016 - 5:00 AM

  1. #!/usr/bin/env python

  2. l=[]
  3. for r in open('file').readlines():
  4. l.append(int(r.split()[1]))

  5. a,b=l[0:]
  6. print a,b

Posted by Irene at March 30, 2016 - 5:30 AM

Thank you, but line7 error.

Posted by Harlan at April 07, 2016 - 6:07 AM

What you said two value assigned to the two variables, the following code will only your file file is only 2
  1. [root@ py]$ cat file
  2. test01 98
  3. test02 87
  4. [root@ py]$ cat 56.py
  5. #!/usr/bin/env python

  6. l=[]
  7. for r in open('file').readlines():
  8. l.append(int(r.split()[1]))

  9. a,b=l[0:]
  10. print a,b
  11. [root@ py]$ python 56.py
  12. 98 87

Posted by Irene at April 12, 2016 - 6:42 AM

Thank you for reply.

My problem is not clearly described, if it is four or more lines in the file, and the value assigned to a number of lines the same number of variables, how to change? Thanks a million.

Posted by Harlan at April 22, 2016 - 7:36 AM

  1. [root@ py]$ cat file
  2. test01 98
  3. test02 87
  4. test03 40
  5. [root@ py]$ cat 56.py
  6. #!/usr/bin/env python

  7. l=[]
  8. value={}
  9. i=1
  10. for r in open('file').readlines():
  11. value[i]=int(r.split()[1])
  12. i+=1
  13. for i in value:
  14. print value[i],
  15. [root@ py]$ python 56.py
  16. 98 87 40

Posted by Irene at April 26, 2016 - 8:29 AM

Thank God, worship. . .

Posted by Harlan at April 28, 2016 - 9:26 AM