二进制形式操作文件rb、wb、ab

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/bus_lupe/article/details/84451449

操作文件默认的是text文本,除了文本之外还有图片、视频等格式的文件。另外,使用二进制操作文件可以跨平台。

rb

# 二进制读文件不需要指定编码方式
f = open('data', 'rb')
res = f.read()
f.close()
print(res)
# b'\xe5\xbf\xab\xe4\xb9\x90\xe7\x9a\x849\xe5\x8f\xaa\xe5\xb0\x8f\xe8\xb7\xb3\xe8\x9b\x99'
print(res.decode(encoding='utf-8'))
# 快乐的9只小跳蛙

wb

二进制写操作需要将字符串编码

f = open('data', 'wb')
f.write('百鸟朝凤\n'.encode('utf-8'))
f.write(bytes('百鸟朝凤\n', encoding='utf-8'))

ab

f = open('data', 'ab')
f.write('百鸟朝凤\n'.encode('utf-8'))
f.write(bytes('百鸟朝凤\n', encoding='utf-8'))
展开阅读全文

二进制形式的文件读取,高手请进!

04-13

/// rn /// 将二进制形式的文件内容转化为文本形式rn /// rn /// 文件地址rn /// stringrn public static string GetStringFromFileByBin(string FileAddress)rn rn string Content="";rnrn IFormatter formatter = new BinaryFormatter();rnrn Stream stream = new FileStream(FileAddress, FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read);rnrn if (stream.Length.ToString().Equals("0"))rn rn Content = "";rn rn elsern rn Content = formatter.Deserialize(stream).ToString();rn rn stream.Close();rnrn return Content;rn rnrn结果:rn2006-3-21 15:37:120$[308|1.472547][307|0][304|0][91|10.272][94|0][93|0][92|3.934818][402|346.8741][90|0][89|9.91992][88|9.91992][294|1][293|0.1][303|0.4347826][302|0.9582106][301|0.1][86|0][85|0][84|0][83|0][82|0][81|0][80|0][79|0][292|2.420341][290|1.01][71|20.13235][70|20.81129][69|13.91578][68|19.96511][67|86.29601][165|4.712885][170|28.70777][486|338][55|45.00009][54|54][53|537.5][262|0][261|0.004199264][46|0.2241487][401|4239194][256|-0.002713449][40|0.7822266][144|41.93539][442|1.450521][27|3.03796][26|0.3209189][25|51.23565][24|3.368012][23|20.88557][22|187.3083][21|1.357601][20|0.6617197][17|0][16|90][15|1466.379][14|34][11|4.580729][9|20.70567][7|36.08345][6|900][3|1276][1|4165.782][400|223115.7]$#rn本来文件中有好条这样的记录,可是现在只能读出一条。rn原文件内容:rn    ?2006-3-21 15:37:120$[308|1.472547][307|0][304|0][91|10.272][94|0][93|0][92|3.934818][402|346.8741][90|0][89|9.91992][88|9.91992][294|1][293|0.1][303|0.4347826][302|0.9582106][301|0.1][86|0][85|0][84|0][83|0][82|0][81|0][80|0][79|0][292|2.420341][290|1.01][71|20.13235][70|20.81129][69|13.91578][68|19.96511][67|86.29601][165|4.712885][170|28.70777][486|338][55|45.00009][54|54][53|537.5][262|0][261|0.004199264][46|0.2241487][401|4239194][256|-0.002713449][40|0.7822266][144|41.93539][442|1.450521][27|3.03796][26|0.3209189][25|51.23565][24|3.368012][23|20.88557][22|187.3083][21|1.357601][20|0.6617197][17|0][16|90][15|1466.379][14|34][11|4.580729][9|20.70567][7|36.08345][6|900][3|1276][1|4165.782][400|223115.7]$#    ?2006-3-21 15:37:170$[308|1.472547][307|0][304|0][91|10.272][94|0][93|0][92|5.247958][402|346.8741][90|0][89|9.91992][88|9.91992][294|1][293|0.1][303|0.4347826][302|0.9582106][301|0.1][86|0][85|0][84|0][83|0][82|0][81|0][80|0][79|0][292|2.420341][290|1.01][71|10.03819][70|9.021][69|4.715671][68|5.8339][67|35.9272][165|4.712885][170|28.70777][486|338][55|45.00009][54|54][53|537.5][262|0][261|0.004199264][46|0.2241487][401|4239194][256|-0.005740372][40|0.7949219][144|41.93539][442|1.450521][27|1.28675][26|0.3067189][25|3.133608][24|6.804008][23|3.974348][22|187.3083][21|0.1858515][20|0.2774088][17|0][16|90][15|72.77682][14|34][11|4.580729][9|20.70567][7|36.07092][6|900][3|1276][1|4165.795][400|223115.7]$#    ?2006-3-21 15:37:220$[308|1.472547][307|0][304|0][91|10.272][94|0][93|0][92|5.930865][402|346.9854][90|0][89|9.91992][88|9.91992][294|1][293|0.1][303|0.4347826][302|0.9582106][301|0.1][86|0][85|0][84|0][83|0][82|0][81|0][80|0][79|0][292|2.420341][290|1.01][71|7.424203][70|7.609295][69|3.850988][68|3.024132][67|25.87103][165|4.712885][170|28.70767][486|338][55|45.00009][54|54][53|537.5][262|0]........ 论坛

没有更多推荐了,返回首页