Basic Python programming code exercises (7)

1. Read the json file

Create a json file containing the following information ( note that the address is a json object, including country and city ), please use the method of reading the json file in Python to obtain and print the name, age and city information in the json object

My name is Xiaoming , I am  18  years old , male ,  I like  listening to music ,  games , eating , sleeping , playing Doudou ,

My residential address is  the country China ,  the city Shanghai .

My name is Xiaohong , I am  17  years old , female ,  I like  listening to music ,  studying , shopping

My residential address is  the country  China ,  the city Beijing .

The implementation code is as follows:

import json
with open("info2.json",encoding="utf-8") as f:
 info_list = json.load(f)
for info in info_list:
 print('我叫',info.get('name'),',我今年',info.get('age'),'岁,性别',info.get('sex')
       ,'爱好',info.get('like'),'我的居住地址为 国家',info.get('address').get('country'),'城市',info.get('address').get('city'))

info2.json code:

[
{
"name":"小明",
"age":18,
"sex":"男",
"isMen":true,
"like":["听歌","游戏","购物","吃饭","睡觉","打豆豆"],
"address":{
"country":"中国",
"city":"上海"
}
},
{
"name":"小红",
"age":17,
"sex":"女",
"isMen":false,
"like":["听歌","学习"],
"address":{
"country":"中国",
"city":"北京"
}
}
]

operation result:

 

2. Automation parameterization problem

The test data of a website is as follows: data.json, requirements, extract the user name, password and expected results in the json file, and form the following format: [(), (), ()] (data format required for automatic parameterization)

[

   {

      "desc": "Correct username and password",

      "username": "admin",

      "password": "123456",

      "expect": "Login successful"

    },

    {

      "desc": "Wrong username",

      "username": "root",

      "password": "123456",

      "expect": "Login failed"

    },

    {

      "desc": "Wrong Password",

      "username": "admin",

      "password": "123123",

      "expect": "Login failed"

    }

]

The implementation code is as follows:

import json
my_list = [('admin','123456','登录成功'),('root','123456','登录失败'),('admin','123123','登录失败')]
with open('info3.json','w',encoding='utf-8') as f:
  json.dump(my_list,f,ensure_ascii=False,indent=2)

info3.json code:

[
  [
    "admin",
    "123456",
    "登录成功"
  ],
  [
    "root",
    "123456",
    "登录失败"
  ],
  [
    "admin",
    "123123",
    "登录失败"
  ]
]

3. Output the course name according to the number

  1. Enter any number between 1 and 3 as prompted by the console , and the program will output the corresponding course name
  2. Judge based on keyboard input. If the input is correct, output the corresponding course name. If the input is wrong, give an error message
  3. Regardless of whether the input is correct or not, output the sentence "Suggestions are welcome"

 The implementation code is as follows:

def inputs():
    list=['python课程','Diango课程','Flask课程']
    try:
        courseId=int(input('请输入课程代号(1-3之间的数字):'))
    except ValueError:
        print('输入格式不正确')
    else:
        if courseId<1 or courseId>3:
            print('范围必须在1-3之间')
        else:
            print(list[courseId-1])
    finally:
        print('欢迎提出建议')
inputs()

operation result:

 

4. File reading and writing

Step 1. Create a txt file containing personal information in the project through the method of reading and writing Python files, which contains your personal profile.

Step 2 Use code to add a line of information at the end of the file "I think cfy is super handsome!"

The implementation code is as follows:

with open(r'123.txt',mode="ta",encoding="utf-8") as ta:
     ta.write("我觉得cfy最帅!\n")

Create a new 123.txt and write it into the text

5. File reading and writing

Use Python to read file information, get the content in the personal information file just created through code, output and print it on the console

The implementation code is as follows:

with open('111.txt') as file_obj:
    content = file_obj.read()
    print(content)

111.txt code:

my name is joker,
I am 18 years old,
How about you?

operation result:

 

Guess you like

Origin blog.csdn.net/qq_63010259/article/details/130612761