实际编程和面试都会遇到的典型问题。
如何读写文本文件
# python2
s = u'你好'
f = open('py2.txt','w')
f.write(s.encode('gbk'))
f.close()
f = open('py2.txt','r')
t = f.read()
f.close()
print t.decode('gbk')
# python3
f = open('py3.txt','wt',encoding='utf-8')
f.write('你好')
f.close()
f = open('py3.txt','rt',encoding='utf-8')
s = f.read()
f.close()
print(s)
如何设置文件的缓冲
# 全缓冲
# 默认是4096
f = open('demo.txt','w',buffering=2048)
f.write('-' * 2048)
f.write('+')
# 行缓冲
f = open('demo1.txt','w',buffering=1)
# 无缓冲
f = open('demo2.txt','w',buffering=0)
如何访问文件的状态
import os
import stat
#第一种 直接使用系统调用
# 1 文件的类型
s = os.stat('demo.txt')
stat.S_ISDIR(s.st_mode)
stat.S_ISREG(s.st_mode)
# 2 文件的访问权限
s.st_mode & stat.S_IXUSR
# 3 文件的最后的访问/修改/节点状态更改时间
import time
time.localtime(s.st_atime)
time.localtime(s.st_mtime)
time.localtime(s.st_ctime)
# 4 普通文件的大小
s.st_size
#第二种使用os.path下的函数
os.path.isdir('demo.txt')
os.path.isfile('demo.txt')
os.path.getatime
如何使用临时文件
from tempfile import TemporaryFile,NamedTemporaryFile
f = TemporaryFile()
f.write(b'acvbnm'*100)
f.seek(0)
f.read(2)
ntf = NamedTemporaryFile()
ntf.name
如何读写csv文件
from urllib.request import urlretrieve
# 下载平安银行2017-04-01到2017-10-20的数据
#日期,股票代码,名称,收盘价,最高价,最低价,开盘价,前收盘,涨跌额,涨跌幅,换手率,成交量,成交金额,总市值,流通市值,成交笔数
urlretrieve('http://quotes.money.163.com/service/chddata.html?code=1000001&start=20170401&end=20171020','pingan.csv')
with open('pingan.csv','rt',encoding = 'gbk') as rf:
reader = csv.reader(rf)
with open('pingan_copy.csv','wt') as wf:
writer = csv.writer(wf)
headers = next(reader)
writer.writerow(headers)
for row in reader:
# 提取出2017-10-01后 成交额大于5000W的数据
if row[0] < '2017-10-01':
break
if round(float(row[12])) >= 50000000:
writer.writerow(row)
print('end')
如何读写json文件
import json
import requests
url = 'http://wthrcdn.etouch.cn/weather_mini?city=北京'
response = requests.get(url)
# json.loads: str转成dict
res = json.loads(response.content.decode('utf-8'))
type(res)
weather_forecast = res['data']['forecast'][0]
# json.dumps: dict转成str
l = json.dumps(res)
type(l)
如何构建并解析xml文件
from xml.etree.ElementTree import Element,ElementTree,tostring,parse
e = Element('collection')
e.set('shelf','New Arrivals')
e1 = Element('movie')
e1.set('title','Enemy Behind')
e11 = Element('type')
e11.text = 'War, Thriller'
e1.append(e11)
e.append(e1)
e2 = Element('movie')
e2.set('title','Enemy Behind')
e21 = Element('type')
e21.text = 'War, Thriller'
e2.append(e21)
e.append(e2)
e3 = Element('movie')
e3.set('title','Enemy Behind')
e31 = Element('type')
e31.text = 'War, Thriller'
e3.append(e31)
e.append(e3)
e4 = Element('movie')
e4.set('title','Enemy Behind')
e41 = Element('type')
e41.text = 'War, Thriller'
e4.append(e41)
e.append(e4)
et = ElementTree(e)
et.write('demo.xml')
f = open('demo.xml')
et = parse(f)
root = et.getroot()
# for child in root:
# print(child.get('title'))
#只能寻找当前元素的直接子元素
root.find('movie[@title]')
root.findall('movie')
root.findall('movie[1]')
root.findall('movie[@title="Ishtar"]')
root.findall('movie[type="Comedy"]')
list(root.iter('movie'))
如何读写excel文件
import xlrd,xlwt
# 读
rbook = xlrd.open_workbook('demo.xlsx')
sheet = rbook.sheet_by_index(0)
# 列数
sheet.ncols
# 行数
sheet.nrows
# 单元格 cell
cell = sheet.cell(0,0)
# 返回一行
sheet.row(0)
sheet.row_values(1)
# 返回一列
sheet.col(0)
sheet.col_values(1)
# 写
wbook = xlwt.Workbook()
wsheet = wbook.add_sheet('sheet1')
# 写入cell
wsheet.write(0,0,'123')
wbook.save('output.xlsx')