numpy中有两个函数可以用来读取文件,主要是txt文件, 下面主要来介绍这两个函数的用法

第一个是loadtxt, 其一般用法为

numpy.loadtxt(fname, dtype=, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)

上面给出了loadtxt所有的关键字参数, 这里我们可以来一一解释并给出示例

这里我们使用的是jupyter notebook, 可以实现交互式的界面操作

%%writefile test.txt # 这是用来写入文件的代码
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7

首先给出最简单的loadtxt的代码

import numpy as np
a = np.loadtxt('test.txt')#最普通的loadtxt
print(a)

实际上就是直接写文件名, 其他关键字参数都是默认的。输出为

[[1. 2. 3. 4.]
[2. 3. 4. 5.]
[3. 4. 5. 6.]
[4. 5. 6. 7.]]
a为浮点数的原因为Python默认的数字的数据类型为双精度浮点数
%%writefile test.txt
A B C
1 2 3
4 5 6
7 8 9 a = np.loadtxt('test1.txt', skiprows=1, dtype=int)
print(a)

这里的skiprows是指跳过前1行, 如果设置skiprows=2, 就会跳过前两行,  这里的输出为

[[1 2 3]
[4 5 6]
[7 8 9]]
%%writefile test.txt
A B C
1 2 3
# AAA
4 5 6
7 8 9 a = np.loadtxt('test2.txt', dtype=int, skiprows=1, comments='#')
print(a)

这里的comment的是指, 如果行的开头为#就会跳过该行, 这里输出为

[[1 2 3]
[4 5 6]
[7 8 9]]
%%writefile test.txt
A B C
1, 2, 3
# AA AAA
4, 5, 6
7, 8, 9 (a, b) = np.loadtxt('test.txt', dtype=int, skiprows=1, comments='#', delimiter=',', usecols=(0, 2), unpack=True)
print(a, b)

这里的usecols是指只使用0,2两列, unpack是指会把每一列当成一个向量输出, 而不是合并在一起。

[1 4 7] [3 6 9]

最后介绍converters参数, 这个是对数据进行预处理的参数, 我们可以先定义一个函数, 这里的converters是一个字典, 表示第零列使用函数add_one来进行预处理
def add_one(x):
return int(x)+1#注意到这里使用的字符的数据结构
(a, b) = np.loadtxt('test.txt', dtype=int, skiprows=1, converters={0:add_one}, comments='#', delimiter=',', usecols=(0, 2), unpack=True)
print(a, b)
输出结果为:
[2 5 8] [3 6 9]

补一个GitHub的jupyter-notebook链接...
https://github.com/ChangChunHe/PythonLearning/blob/master/Numpy/8.loadtxt_and_genfromtxt.ipynb

numpy中loadtxt 的用法的更多相关文章

  1. Python numpy中矩阵的用法总结

    关于Python Numpy库基础知识请参考博文:https://www.cnblogs.com/wj-1314/p/9722794.html Python矩阵的基本用法 mat()函数将目标数据的类 ...

  2. Numpy中 arange() 的用法

    1. 概述Numpy 中 arange() 主要是用于生成数组,具体用法如下: 2. arange()2.1 语法numpy.arange(start, stop, step, dtype = Non ...

  3. python类库numpy中常见函数的用法

    1. numpy.reshape  重塑 reshape是一种函数,函数可以重新调整矩阵的行数.列数.维数. B = reshape(A,m,n) 返回一个m*n的矩阵B, B中元素是按列从A中得到的 ...

  4. numpy中argsort函数用法

    在Python中使用help帮助 >>> import numpy >>> help(numpy.argsort) Help on function argsort ...

  5. numpy中的norm用法

    np.linalg.norm() computes the norm of a NumPy array according to an order, ord, which specifies the ...

  6. numpy中tile的用法

    a=arange(1,3) #a的结果是: array([1,2]) 1,当 tile(a,1) 时: tile(a,1) #结果是 array([1,2]) tile(a,2) #结果是 array ...

  7. 【python】numpy中的shape用法

    转自 https://blog.csdn.net/u010758410/article/details/71554224# shape函数是numpy.core.fromnumeric中的函数,它的功 ...

  8. Numpy中Meshgrid函数介绍及2种应用场景

    近期在好几个地方都看到meshgrid的使用,虽然之前也注意到meshgrid的用法.但总觉得印象不深刻,不是太了解meshgrid的应用场景.所以,本文将进一步介绍Numpy中meshgrid的用法 ...

  9. OpenCV中cv2的用法

    一.读入图像 使用函数cv2.imread(filepath,flags)读入一副图片 filepath:要读入图片的完整路径 flags:读入图片的标志  cv2.IMREAD_COLOR:默认参数 ...

随机推荐

  1. spring mvc 跨域请求处理——spring 4.2 以上

    Controller method CORS configuration You can add to your @RequestMapping annotated handler method a  ...

  2. 【大数据系列】基于MapReduce的数据处理 SequenceFile序列化文件

    为键值对提供持久的数据结构 1.txt纯文本格式,若干行记录 2.SequenceFile key-value格式,若干行记录,类似于map 3.编写写入和读取的文件 package com.slp; ...

  3. ubuntu14.04 LTS Python IDE专用编辑器PyCharm开发环境搭建

    https://www.zhihu.com/question/20381207   有哪些值得推荐的 Python 开发工具? 一 PyCharm下载 官网下载地址:https://www.jetbr ...

  4. js ==和===以及!= 和 !==的区别

    一.js == 与 === 的区别[转] 1. 对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型 ...

  5. Oracle SQL Developer保持数据库连接的方法

    一.概述 从navicat切到pl/sql developer,但是发现个bug,因为本地客户端pl/sql developer的字符集 和数据库服务器的字符集不一致,导致一个很奇葩的东西. 本来我有 ...

  6. 安装Windows Server 2012 R2提示"unable to create a new system partition or locate an existing system partition"解决方法

    重新安装Windows Server 2012 R2,把原来SSD分区全部格式化重建,用U盘启动安装时提示如下: "Setup was unable to create a new syst ...

  7. Python 安装出错:Setup script exited with error: command 'gcc' failed with exit status 1

    退出当前环境: logout (再重新登录进去) yum install python-devel  -yyum install libevent-devel  -y 把环境更新下yum instal ...

  8. 使用正则表达式来截取nginx中的内置变量

    nginx 中的内置变量都可以通过 if 指令 + 正则表达式来进行截取,截取之后的结果通过正则表达式的分组来进行引用 比如:从请求中传过来的一个名为 ssl_client_s_dn 的变量,它的值是 ...

  9. MacOS 安装 nginx

    brew install nginx 开机启动 $ sudo cp `brew --prefix nginx`/homebrew.mxcl.nginx.plist /Library/LaunchDae ...

  10. 【BZOJ2658】[Zjoi2012]小蓝的好友(mrx) 平衡树维护笛卡尔树+扫描线

    [BZOJ2658][Zjoi2012]小蓝的好友(mrx) Description 终于到达了这次选拔赛的最后一题,想必你已经厌倦了小蓝和小白的故事,为了回馈各位比赛选手,此题的主角是贯穿这次比赛的 ...