Share the content returned by the python processing system, and find the small code to output the desired field through the conversion between various data types. I think it’s very useful for learning data types.

Time:2021-3-18

Script effect:

When the input parameter is Xiaohong, the gender of Xiaohong will be output. When the input parameter is Xiaoming, the gender of Xiaoming will be output,

Learning purpose:

Through tuple, list, dictionary, string conversion, used to deal with possible large text string filtering

code

[email protected]  ~/python  cat shujuleixing.py
#!/usr/bin/python3
#coding=utf-8
import os
import subprocess
import sys

name=sys.argv[1]
cmd="cat /root/python/class.txt"
g=[]
def out_put(cmd):
    p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding='utf-8')
    stdout=p.communicate()
    return stdout
def step1(cmd):
    a=out_put(cmd)
    b=a[0]
    c=eval(b)
    for d in c:
        e=d['class']
        f=eval(e)
        g.append(f)
    print('a---',type(a),a)
    print('b---',type(b),b)
    print('c---',type(c),c)
    print('d---',type(d),d)
    print('e---',type(e),e)
    print('f---',type(f),f)
    print('g---',type(g),g)
def step2(cmd):
    step1(cmd)
    for h in g:
        if name==h['name']:
            i=h['sex']
        elif name==h['name']:
            i=h['sex']
    print('h---',type(h),h)
    print('i---',type(i),i)
step2(cmd)

text

[email protected]  ~/python cat class.txt
[{"class":"{\"name\":\"xiaoming\",\"sex\":\"male\"}"},{"class":"{\"name\":\"xiaohong\",\"sex\":\"female\"}"}]

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Hash algorithm

In 2011, CSDN’s “off database” incident, at that time, CSDN’s website was attacked by hackers, and the registered mailbox and password plaintext of more than 6 million users were leaked. Many netizens were dissatisfied with CSDN’s plaintext saving of user passwords. If you were an engineer of CSDN, how would you store such important data […]