Python关于File学习过程
一、首先,认识下文件
进行个总结:
计算机内的文件广义上来说,只有二进制文件
狭义上来讲分为两大类:二进制文件和文本文件。
先说数据的产生(即写操作)
文本文件的所有数据都是固定长度的,每条数据(也就是每个字符)都是1个字节。文本文件的“编/解码器”会将每条数据转换成ASCII码或者Unicode,然后以二进制的形式存到硬盘;
而二进制文件每条数据不固定,如short占2个字节,int占5个字节,float占8个字节(不一定,只是举个例子),这是二进制文件的写操作是将内存里的数据直接写入文件。
再说数据的读取:
文件的读过程是这样的:磁盘 》》 文件缓冲区》》应用程序内存空间。
我们说“文本文件和二进制文件没有区别”,实际上针对的是第一个过程;既然没有区别,那么打开方式不同,为何显示内容就不同呢?这个区别实际上是第二个过程造成的。
文件实际上包括两部分,控制信息和内容信息。纯文本文件仅仅是没有控制格式信息罢了;
1.以Numpy的multiarray.fromfile为例
numpy.fromfile()
def fromfile(file, dtype=None, count=-1, sep=''): # real signature unknown; restored from __doc__
"""
fromfile(file, dtype=float, count=-1, sep='') Construct an array from data in a text or binary file. A highly efficient way of reading binary data with a known data-type,
as well as parsing simply formatted text files. Data written using the
`tofile` method can be read using this function. Parameters
----------
file : file or str
Open file object or filename.
dtype : data-type
Data type of the returned array.
For binary files, it is used to determine the size and byte-order
of the items in the file.
count : int
Number of items to read. ``-1`` means all items (i.e., the complete
file).
sep : str
Separator between items if file is a text file.
Empty ("") separator means the file should be treated as binary.
Spaces (" ") in the separator match zero or more whitespace characters.
A separator consisting only of spaces must match at least one
whitespace. See also
--------
load, save
ndarray.tofile
loadtxt : More flexible way of loading data from a text file. Notes
-----
Do not rely on the combination of `tofile` and `fromfile` for
data storage, as the binary files generated are are not platform
independent. In particular, no byte-order or data-type information is
saved. Data can be stored in the platform independent ``.npy`` format
using `save` and `load` instead. Examples
--------
Construct an ndarray: >>> dt = np.dtype([('time', [('min', int), ('sec', int)]),
... ('temp', float)])
>>> x = np.zeros((1,), dtype=dt)
>>> x['time']['min'] = 10; x['temp'] = 98.25
>>> x
array([((10, 0), 98.25)],
dtype=[('time', [('min', '<i4'), ('sec', '<i4')]), ('temp', '<f8')]) Save the raw data to disk: >>> import os
>>> fname = os.tmpnam()
>>> x.tofile(fname) Read the raw data from disk: >>> np.fromfile(fname, dtype=dt)
array([((10, 0), 98.25)],
dtype=[('time', [('min', '<i4'), ('sec', '<i4')]), ('temp', '<f8')]) The recommended way to store and load data: >>> np.save(fname, x)
>>> np.load(fname + '.npy')
array([((10, 0), 98.25)],
dtype=[('time', [('min', '<i4'), ('sec', '<i4')]), ('temp', '<f8')])
"""
pass
值得注意的是,
Empty ("") separator means the file should be treated as binary.
也就是说,default情况下,是将文件按照二进制文件读取的,加上separator参数后会将二进制转换后的ASCII码或者unicode再解码为文本数据,
以test.txt文件为例(1对应的ASCII码十进制为49,","为44)
test.txt
1,1,1,1,1
(1)使用默认sep参数读取:
filepath = "D://Documents/temp/testForPyStruct.txt"
data= np.fromfile(filepath , dtype=np.uint8, sep="")
print(data)
输出
[49 44 49 44 49 44 49 44 49]
(2)使用sep=","读取:
filepath = "D://Documents/temp/testForPyStruct.txt"
data= np.fromfile(filepath , dtype=np.uint8, sep=",")
print(data)
输出
[1 1 1 1 1]
2.
See also
--------
load, save
ndarray.tofile
loadtxt : More flexible way of loading data from a text file.
Python关于File学习过程的更多相关文章
- 转发 python中file和open有什么区别
python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...
- python爬取豆瓣小组700+话题加回复啦啦啦python open file with a variable name
需求:爬取豆瓣小组所有话题(话题title,内容,作者,发布时间),及回复(最佳回复,普通回复,回复_回复,翻页回复,0回复) 解决:1. 先爬取小组下,所有的主题链接,通过定位nextpage翻页获 ...
- Python模块File文件操作
Python模块File简介 Python提供了File模块进行文件的操作,他是Python的内置模块.我们在使用File模块的时候,必须先用Popen()函数打开一个文件,在使用结束需要close关 ...
- Non-Programmer's Tutorial for Python 3/File IO
File I/O Here is a simple example of file I/O (input/output): # Write a file with open("test.tx ...
- python read file(f,csv)
import csv def readfile0(): print('test read file') in_file = open('C:\python\demo\LiaoXueFeng\data\ ...
- rc.local 注意事項,call python script, file position
如果要在 rc.local 呼叫 python script python script 的位置需使用絕對路徑 其 python script 裡的有關 file 的位置也需使用 絕對路徑 如果要在 ...
- python之file 方法
file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 1 file.close() close() 方法用于关闭一个已打开的文件.关闭后的文件不能再进行读写操作, 否 ...
- 15.python文件(file)方法详解
文件的基本操作 文件读写: 文件的读写满足以下3个步骤: 1).打开文件 2).操作数据(读.写) 3).关闭文件 --> 不要忘记 1).打开文件: python的open() 方法用于打开一 ...
- Python Socket File Transfer
I have a RPi which I intented to use it to crawl data. The development environment in RPi is very ba ...
随机推荐
- LLVM使用其他Pass的结果
之前的工作一直集中在clang中,最近有点空闲时间,又重新熟悉了一下Pass的书写过程.(参考LLVM CookBook和http://llvm.org/docs/WritingAnLLVMPass. ...
- JDK,JRE,JVM 关系和概念
JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...
- STM32点亮LED
原理图 测试灯,接GPIO外设B,Pin 12 举例 前提,工程模版建立好 #include "stm32f10x.h" void delay(u32 i) { while(i-- ...
- Oracle笔记(十一) 建表、更新、查询综合练习
有某个学生运动会比赛信息的数据库,保存了如下的表: 运动员sporter(运动员编号sporterid,运动员姓名name,运动员性别sex,所属系号department) 项目item(项目编号it ...
- [Abp vNext微服务实践] - 启动流程
前几篇分别介绍了abp vNext微服务框架和微服务CI/CD环境搭建,本篇开始介绍vNext微服务框架的开发环境搭建. 环境准备 官方介绍的系统架构图如下: 上图中身份服务和网关服务已经集成在系统中 ...
- java -为什么重写equals(),还需要重写hashCode()?
1.先post这两个方法的基本定义: equals()的定义: 浅谈Java中的equals和==(转) hashCode()的定义: java中hashCode()方法的作用 Java中hashCo ...
- Zabbix trigger(触发器)设置
设置一个监控项–进站包数,当进站包数>50触发器报警. 先设置一个进站包数的监控项(item):
- c++中lambda表达式的用法
#include <iostream> using namespace std; int main(){ ; auto func1 = [=](;}; auto func2 = [& ...
- MariaDb 严格默认严格模式导致有 NULL 值新增失败 (sql_model STRICT)
分析: 由于 MaridDb 默认工作在严格模式下,所以导致无法 Insert 解决: 1 新增自定义配置 /etc/mysql/mariadb.conf.d/50-disable_strict_mo ...
- DockerAPI版本不匹配的问题
1.问题描述 在执行docker指令的时候显示client和server的API版本不匹配,如下: 说明:在这里server API的版本号比client API版本号低,因此不能有效实现cilent ...