繁体   English   中英

numpy-Python-选择性导入.txt文件的一部分

[英]numpy - Python - Selectively import parts of the .txt file

在我的data.txt文件中,有2种类型的行。

  1. 普通数据:16个数字,中间用空格分隔,并在末尾添加“ \\ n”。

  2. 数据不完整:在将数据写入data.txt的过程中,最后一行的写入始终被STOP命令中断。 因此,它始终是不完整的,例如它可以有10个数字并且没有'\\ n'

两个问题:

一种。 除了最后一个不完整的行,如何将整个文件导入Python?

我注意到

# Load the .txt file in
myData = np.loadtxt('twenty_z_up.txt')

在存在最后一个不完整的行的情况下,该文件是“严格的”,无法导入文件。 导入的.txt文件必须是一个不错的矩阵。

有时,出于实验目的,我会在一行的第一个条目上加上时间戳。 假设我在第2行的开始处有我的第一个时间戳,在第5行的开始处有我的第二个戳记。 我如何仅从第2行到第5行导入Python?

==============================更新:Qa已解决=============== ==================

myData = np.genfromtxt('fast_walking_pocket.txt', skip_footer=1)

将有助于丢弃最后的不完整行

你可以尝试大熊猫提供一个使用功能read_csv更容易地加载数据。

示例数据:

a b c d e f g h i j k l m n o p
a b c d e f g h i j k l m n o p
a b c d e f g h i j k l m n o p
a b c d e f g h i j k l m n o p
a b c d e f g h i j k l m n o p
a b c d e f g h i j

对于Q1,您可以通过以下方式加载数据:

In [27]: import pandas as pd

In [28]: df = pd.read_csv('test.txt', sep=' ', header=None, skipfooter=1)

DataFrame是有用的结构,可以帮助您更轻松地处理数据。 要获得一个numpy数组,只需获取DataFramevalues属性。

In [33]: df.values
Out[33]: 
array([['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p'],
       ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p'],
       ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p'],
       ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p'],
       ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
        'n', 'o', 'p']], dtype=object)

对于第二季度,您可以通过

In [36]: df.ix[[1, 4]]
Out[36]:
  0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15
1  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p
4  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p

回答您的“ b”问题。

假设您有以下文件(称为“ /tmp/lines.txt”):

line 1
2013:10:15
line 3
line 4
2010:8:15
line 6 

您可以使用linecache模块:

>>> import linecache
>>> linecache.getline('/tmp/lines.txt', 2)
'2013:10:15\n'

因此,您可以直接对此时间进行解析:

>>> import datetime as dt
>>>dt.datetime.strptime(linecache.getline('/tmp/lines.txt',2).strip(),'%Y:%m:%d')
datetime.datetime(2013, 10, 15, 0, 0)

编辑

多行:

>>> li=[]
>>> for i in (2,5):
...    li.append(linecache.getline('/tmp/lines.txt', i).strip())
... 
>>> li
['2013:10:15', '2010:8:15']

要么:

>>> lines={}
>>> for i in (2,5):
...    lines[i]=linecache.getline('/tmp/lines.txt', i).strip()
... 
>>> lines
{2: '2013:10:15', 5: '2010:8:15'}

或范围:

>>> lines={}
>>> for i in range(2,6):
...    lines[i]=linecache.getline('/tmp/lines.txt', i).strip()
... 
>>> lines
{2: '2013:10:15', 3: 'line 3', 4: 'line 4', 5: '2010:8:15'}

问题一:

np.genfromtxt('twenty_z_up.txt',skip_footer=1)

问题b:

np.genfromtxt('twenty_z_up.txt',skip_footer=1)[2:5]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM