在线客服
扫描二维码
下载博学谷APP扫描二维码
关注博学谷微信公众号
大家在使用Python的时候,如果知道一些小技巧,那么工作效率会有很大的提高。小编整理了关于文件读写的Python技巧,都是干货,希望能对大家有一点小小的帮助吧!
# python2 str unicode
# python3 bytes str
# python2
s = u'你好'
s.encode('utf8') #存储到文件中的格式
f = open('hello.txt', 'w')
f.write(s.encode('utf8'))
f.close()
f = open('hello.txt', 'r')
t = f.read().decode('utf8') # 你好
f.close()
# python3 字符串就是unicode
strb = b'asdfasdfsdg'
s = '你好'
f = open('hello2.txt', 'wt', encoding='utf8') # 自动完成编解码
f.write(s)
f.close()
f = open('hello2.txt', 'rt', encoding='utf8')
s = f.read()
f.close()
# 处理二进制文件 处理音频文件,将音量调小保存
f = open('demo.wav', 'rb')
info = f.read(44) #文件头
import struct
struct.unpack('h',info[22:24]) #处理文件头 数据运算
struct.unpack('i',infi[24:28])
f.seek(0,2)
f.tell()
n = (f.tell()-44) /2
import array
buf = array.array('h', (0 for _ in xrange(n)))
f.seek(44)
f.readinto(buf)
for i in xrange(n): buf[i] /= 8
f2 = open('demo2.wav', 'wb')
f2.write(info)
buf.tofile(f2)
f2.close()
# 使用临时文件
# 自动删除,不占内存
from tempfile import TemporaryFile, NamedTemporaryFile
f = TemporaryFile() # 系统文件系统找不到
f.write('abcddee'*100000)
f.seek(0)
f.read(100)
ntf = NamedTemporaryFile(delete=False) # 能找到文件,默认关闭以后会删除文件
fname = nft.name
# 设置文件的缓冲
# I/O 操作以块为单位,如4096字节一个块
f = open('test.txt', 'w', buffering=2048) # 全缓冲,要写满缓冲才会写到文件中
f = open('test.txt', 'w', buffering=1) # 行缓冲,\n就会写文件
f = open('test.txt', 'w', buffering=1) # 无缓冲,实时写
f.write('abc')
# 将文件映射到内存
import mmap
f = open('demo.bn','r+b')
f.fileno()
m = mmap.mmap(f.fileno(), 0, access=mmpa.ACCESS_WRITE, offset=mmap.PAGESIZE)
# 得到字节数组
m[4:8] = '\xff'*4 # 修改直接改变文件内容
# 读写csv数据
from urllib import urlretrieve
urlretrieve('http://table.finance.yahoo.com/table.csv?s=000001.sz', 'pingan.csv')
rf = open('pingan.csv', 'rb')
import csv
reader = csv.reader(rf)
header = reader.next()
wf = open('pingan_c.csv', 'wb')
writer = csv.writeer(wf)
writer.writerow(header)
rf.close()
wf.close()
# 读写json数据
import requests
import json
from record import Record
record = Record(channel=1)
audioData = record.record(2)
from secret import API_KEY, SECRET_KEY
authUrl = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + API_KEY + "&client_secret=" +
SECRET_KEY
response = requests.get(authUrl)
res = json.loads(response.content)
token = res['access_token']
#百度语音识别
cuid = 'xxxxxxxxxxxxx'
srvUrl = 'http://vop.baidu.com/server_api?cuid=' + cuid + '&token=' + token
heepHeader = {'Content-Type': 'audio/wav; rate = 8000'}
response = requests.post(srvUrl, headers=httpHeader, data=audioData)
res = json.loads(response.content)
text = res['result'][0]
print text
# json.dumps() python对象(列表、字典等)转换成json字符串
# json.dumps(data, sort_keys=True)
# json.loads() json字符串转换成python对象
with open('demo.json', 'wb') as f:
json.dump(l, f) # 将l数据写到文件
# 构建xml文档
from xml.etree.ElementTree import parse
with open('demo.xml') with f:
et = parse(f)
root = et.getroot()
root.tag
root.attrib
root.text
#root.getchildren()
for child in root:
print child.get('name')
root.find('country')
root.findall('country') # 直接子元素
for e in root.iterfind('country'):
print e.get('name')
from xml.etree.ElementTree import Element, ElementTree, tostring
e = Element('Data')
e.set('name', 'abc')
e2 = Element('Row')
e3 = Element('Open')
e3.text = '8.80'
e2.append(e3)
e.append(e2)
tostring(e)
et = ElementTree(e)
et.write('demo.xml')
# 读写excel文件
import xlrd, xlwt
book = xlrd.open_workbook('demo.xls')
book.sheets()
sheet = book.sheet_by_index(0)
rows = sheet.nrows
cols = sheet.ncols
cell = sheet.cell(0,0) #(0,0)单元格
cell.ctype
cell.value
row = sheet.row(1) #cell对象列表
data = sheet.row_values(1, 1) #第1列跳过第一格的值列表
sheet.put_cell(0, cols, xlrd.XL_CELL_TEXT, u'Total', None)
wbook = xlwt.Workbook()
wsheet = wbook.add_sheet('sheet1')
style = xlwt.easyxf('align: vertical center, horizontal center')
wsheet.write(rows,cols, sheet.cell_value(rows,cols), style)
wsheet.save('output.xls')
以上就是文件读写Python技巧的整理,觉得本篇文章有用的同学不妨分享出去,让更多人看见。
— 申请免费试学名额 —
在职想转行提升,担心学不会?根据个人情况规划学习路线,闯关式自适应学习模式保证学习效果
讲师一对一辅导,在线答疑解惑,指导就业!
相关推荐 更多
学Python用什么系统?
对于刚刚初学Python的小伙伴来说,一开始学习就会遇到这样的问题,学Python用什么系统?虽说工欲善其事必先利其器,但对于刚刚入门Python的小伙伴来说真的不用太纠结学Python用什么系统。。我的建议是,最好是能花一段时间熟悉了Linux系统以后,再开始转移平台。
7935
2019-07-16 16:50:07
什么要学习Python?Python的优劣势分析
随着人工智能与数据科学技术的普及以及应用,Python成为目前互联网技术开发领域使用率增长最快的编程语言,同时因为其简单易学、入门轻松,成为现在大部分进入互联网的首选语言。那为什么要学习Python?Python有哪些优势呢?
6412
2019-11-11 18:37:48
Python学习笔记之列表的用法总结
众所周知,Python中最基本的数据结构就是序列,而在许多序列的内置类型中,最常见的要属列表了。简单来说,列表作为可以修改的一种序列,它能作为一个方括号内的逗号分隔值出现。本文为大家总结了列表所有的基本用法,下面就一起来看看关于Python列表的学习笔记吧!
5036
2019-12-24 15:17:42
学Python的理由有哪些?这四大理由足够了
学Python的理由有哪些?可能有人会说Python是一种计算机语言,具有简洁性、易读性、及可扩展性,相对于其他语言学起来会更加容易,目前应用也非常广泛等等。其实总结起来,学Python的理由不外乎四点,即丰富免费的模块库,新手入门上手快的学习优势,还有广泛的应用场景以及国家的重视和支持。因此这四大理由足够掀起全面学习Python的热潮。
5397
2020-04-15 11:35:41
Python运算符总结
所有的编程语言本质就是在解决运算逻辑,通过各种算法实现想要的各种功能,因此在学习Python编程语言时,不仅要掌握各种变量类型,深刻理解函数式编程的原理,还要彻底搞懂各类运算符的使用。通过本片文章你可以了解到在Python编程开发中的各类运算符以及其使用方法。
5821
2020-06-08 16:31:37
热门文章
- 前端是什么
- 前端开发的工作职责
- 前端开发需要会什么?先掌握这三大核心关键技术
- 前端开发的工作方向有哪些?
- 简历加分-4步写出HR想要的简历
- 程序员如何突击面试?两大招带你拿下面试官
- 程序员面试技巧
- 架构师的厉害之处竟然是这……
- 架构师书籍推荐
- 懂了这些,才能成为架构师 查看更多
扫描二维码,了解更多信息
