Python-pro
csv
import csv
# encoding='utf-8':设置文件内容编码
# newline='':写入避免双倍行距
def write_data():
with open('stu.csv', 'a+', encoding='utf-8', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Jhone', 23, '0001', '男']) # 单行写入
writer.writerows([{'Jhone', 23, '0001', '男'}, {'Henri', 10, '0002', '女'}]) # 多行写入
def read_data():
with open('stu.csv', 'r', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
for row in reader:
print(row)
if __name__ == '__main__':
# write_data()
read_data()
cookie
import os.path
import urllib
from http import cookiejar
filename = 'cookies.txt'
def file_exists(filename):
if not os.path.exists(filename):
with open(filename, mode='w', encoding='utf-8') as f:
pass
print(f"文件已创建:{filename}")
else:
print(f"文件已存在:{filename}")
def get_cookies():
file_exists(filename)
cookie = cookiejar.MozillaCookieJar(filename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
url = 'https://tieba.baidu.com/index.html?traceid=#'
resp = opener.open(url)
cookie.save(filename)
def use_cookie():
cookie = cookiejar.MozillaCookieJar(filename)
cookie.load(filename)
print(cookie)
if __name__ == '__main__':
get_cookies()
use_cookie()
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 程序员小王
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果