Python如何实现文件读写

发布时间:2021-10-28 15:01:42 作者:小新
来源:亿速云 阅读:162

这篇文章主要为大家展示了“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如何实现文件读写”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. python 文件读写操作(24)
  2. python文件读写操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:在 Exchange 2010 环境中如何安装 Exchange 2003/2007

下一篇:Mysql数据分组排名实现的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》