csv
- csv文件用excel打开是表格形式,用文本文档打开时每个内容用","分割开。
import csv
with open("person.csv",mode='r',newline="") as f:
f_csv = csv.reader(f)
for row in f_csv:
print(row)
csv_writer = csv.writer(f)
csv_writer.writerow([])
csv_writer.writerows([[]])
爬虫
import requests
import pandas as pd
list = ['北京市','天津市','上海市','重庆市','河北省','山西省','辽宁省','吉林省','黑龙江省',
'江苏省','浙江省','安徽省','福建省','江西省','山东省','河南省','湖北省','湖南省','广东省',
'海南省','四川省','贵州省','云南省','陕西省','甘肃省','青海省','台湾省','内蒙古自治区',
'广西壮族自治区','西藏自治区','宁夏回族自治区','新疆维吾尔自治区','香港特别行政区','澳门特别行政区']
url = "https://raw.githubusercontent.com/canghailan/Wuhan-2019-nCoV/master/Wuhan-2019-nCoV.json"
headers = {}
response = request.get(url,headers=headers)
data_json = response.json()
data_df = pd.DataFrame(data_json)
for s in list:
data = data_df[data_df["province"]==s]
data.to_csv("./data/{}.csv".format(s))
...
import urllib.request
from lxml import etree
url = "http://www.dqsy.net/"
headers = {"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}
request = urllib.request.Request(url,headers=headers)
response = urllib.request.urlopen(request)
html_str = response.read().decode("utf-8")
html = etree.HTML(html_str)
imgs = html.xpath("//img/@src")
for img in imgs:
url_img = url + img
urllib.request.urlretrieve(url_img,"e://新建文件夹 (2)/{}".format(img.split("/")[-1]))
os模块
import os
os.makedirs()
from tempfile import TemporaryFile
with TemporaryFile("w+t",prefix="",suffix="",dir="",delete=True) as f:
pass
import shutil
shutil.rmtree("")
import os
for boot,dirs,files in os.walk("f://文件",topdown=False)
import xlrd
from xlutils.copy import copy
book = xlrd.open_workbook("account.xls")
sheet = book.sheet_by_index(0)
print(sheet.nrows,sheet.ncols)
print(sheet.cell(0,0).value)
book = xlrd.open_workbook('account.xls')
book_copy = copy(book)
sheet_copy = book_copy.get_sheet(0)
sheet_copy.write(4,4,0)
book_copy.save('account.xls')