- 写入含有中文的文件。代码含义为在E盘新建一个名为a.txt,且以utf-8编码的文件,并向文件中写入‘中国’。
with open('E:/a.txt','r',encoding='utf-8') as f:
f.write('中国')
- 读取含有中文的文件
with open('E:/a.txt','r',encoding='utf-8') as f:
while True:
print(f.readline())
- 爬取知乎首页并保存为html文件
import requests
url='https://www.zhihu.com'
r=requests.get(url)
with open('E:/zhihu.html') as f:
f.write(r.text)