文章

python-日常操作

Python读写文件

1. open

1
2
3
4
5
6
7
8
9
10
简单使用:
f = open('/Users/michael/test.txt', 'w')
print("Hello world", file=f)
f.write('Hello, world!')
f.close()

复杂加变量: 
f = open("/tmp/fengyl/data_completion_state_"+match_script+".txt",'w')
 print(f"data_completion_state{{spiderName=\"{match_script}\", stats_name=\"{key}\"}} {value}", file=f) 
f.close()

2. with open

1
2
3
4
5
6
7
8
简单使用:
with open('/Users/michael/test.txt', 'w') as f:
    f.write('Hello, world!')
    print("Hello world", file=f)

复杂加变量: 
with open("/tmp/fengyl/data_completion_state_"+match_script+".txt", 'w') as f:
     print(f"data_completion_state{{spiderName=\"{match_script}\", stats_name=\"{key}\"}} {value}", file=f) 

请求内容打印

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
http_url = "https://apollo.abc.com"
data = {
    'username': "aaa",
	'password': '12345',
	'login-submit': '登 录'   
}
response = apollo_session.post(http_url, data=data)

# print request info
print("Request method: ", response.request.method)
print("Request URL: ", response.request.url)
print("Request headers: ", response.request.headers)
# print("Request body: ", response.request.body)  请求完毕会置空,这里没有打印的必要

# print response info
print("Response status code: ", response.status_code)
print("Response headers: ", response.headers)
print("Response body: ", response.text)

本文由作者按照 CC BY 4.0 进行授权