一、首先,认识下文件

文本文件和二进制文件的差异和区别

进行个总结:

计算机内的文件广义上来说,只有二进制文件

狭义上来讲分为两大类:二进制文件和文本文件。

先说数据的产生(即写操作)

文本文件的所有数据都是固定长度的,每条数据(也就是每个字符)都是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学习过程的更多相关文章

  1. 转发 python中file和open有什么区别

    python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...

  2. python爬取豆瓣小组700+话题加回复啦啦啦python open file with a variable name

    需求:爬取豆瓣小组所有话题(话题title,内容,作者,发布时间),及回复(最佳回复,普通回复,回复_回复,翻页回复,0回复) 解决:1. 先爬取小组下,所有的主题链接,通过定位nextpage翻页获 ...

  3. Python模块File文件操作

    Python模块File简介 Python提供了File模块进行文件的操作,他是Python的内置模块.我们在使用File模块的时候,必须先用Popen()函数打开一个文件,在使用结束需要close关 ...

  4. 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 ...

  5. python read file(f,csv)

    import csv def readfile0(): print('test read file') in_file = open('C:\python\demo\LiaoXueFeng\data\ ...

  6. rc.local 注意事項,call python script, file position

    如果要在 rc.local 呼叫 python script python script 的位置需使用絕對路徑 其 python script 裡的有關 file 的位置也需使用 絕對路徑 如果要在 ...

  7. python之file 方法

    file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 1    file.close() close() 方法用于关闭一个已打开的文件.关闭后的文件不能再进行读写操作, 否 ...

  8. 15.python文件(file)方法详解

    文件的基本操作 文件读写: 文件的读写满足以下3个步骤: 1).打开文件 2).操作数据(读.写) 3).关闭文件 --> 不要忘记 1).打开文件: python的open() 方法用于打开一 ...

  9. 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 ...

随机推荐

  1. 使用百度echarts仿雪球分时图(二)

    上一章简单的介绍了一下分时图的构成,其实就是折线图跟柱状图的组成.本来这章打算是把分时图做完,然后再写一章来进行美化和总结,但是仔细观察了一下,发现其实东西还是有点多的.分时图的图表做完后,还要去美化 ...

  2. JAVA线程池例子

    用途及用法 网络请求通常有两种形式:第一种,请求不是很频繁,而且每次连接后会保持相当一段时间来读数据或者写数据,最后断开,如文件下载,网络流媒体等.另 一种形式是请求频繁,但是连接上以后读/写很少量的 ...

  3. SpringCloud之Ribbon负载均衡配置

    一.负载均衡解决方案分类及特征 业界主流的负载均衡解决方案有: 1.1 集中式负载均衡 即在客户端和服务端之间使用独立的负载均衡设施(可以是硬件,如F5, 也可以是软件,如nginx), 由该设施负责 ...

  4. 简单服务器通信 模型socketserver

    硬件服务器:主机 集群 厂商 :IBM   HP  联想  浪潮 软件服务器 :编写的服务端应用程序,在硬件服务器上运行,一般依托于操作系统,给用户提供一套完整的服务 httpserver --> ...

  5. 2.Bacula Server端安装配置

    1.  Bacula Server端安装配置 1.1.  Bacula Server端安装 1.1.1.  安装bacula依赖包 For Centos6: yum install -y mysql ...

  6. PHP面试题--基础

    1.PHP语言的一大优势是跨平台,什么是跨平台?一.PHP基础: PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如windows.Linux等)上配置,不 ...

  7. jQuery表单验证正则表达式-简单

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  8. Linux学习笔记(五)Linux常用命令:压缩命令

    Linux中最常见的5中压缩格式: zip gz bz2 tar.gz tar.bz2 一..zip压缩命令 压缩文件 zip [压缩文件名] [源文件] 例如: zip zijeak.zip zij ...

  9. linux kill某一用户的所有tomcat进程

    1.查看Tomcat进程 ps -ef|grep tomcat  显示当前所有进程: ps -ef|grep tomcat-web  进程中有不同的进程,查询全名,精准kill. 2.根据进程号kil ...

  10. JDK的收费问题

    关于JDK是否收费,我也有过疑问,查了一些资料,就在这里做一个简要的说明. 首先要明白JDK的发布模型.两年多以前,2017年9月21日,伴随着JDK9的发布,JDK就开启了新的发布模式(如下图所示) ...