python - 导入函数参数 numpy

标签 python numpy genfromtxt

我正在尝试导入文本文件 (.xyz),该文件如下所示:

1 9 1 6 "Thu Feb 13 13:12:30 2014     "
0 0 0 0 0 0
38 38 915 915
"CJE                                                                              "
"2                                      "
"110321-025-01D-1ST                    
0 0 1 .1 73.7972 17 50
1 0 7 1 60 0 0 0 0
0 "                           "
1 0
#
38 38 No Data
39 38 No Data
40 38 No Data
41 38 3
42 38 No Data
43 38 4
44 38 4
45 38 5
#

文本文件有一个标题(前 11 行),其中包含一些数值,如下所示,数据也分为三列,其中一列有数值,但也有书面字符:“否数据”。我还想将“无数据”更改为数值 0。

我可以跳过标题,但主要问题是我用它来告诉代码有三列,并且其中“没有数据”意味着 0。 这是我目前用过的,

import numpy as np
data = np.genfromtxt('180228_Test V2-4_0grad.xyz',
                 skip_header=11,
                 skip_footer=1,
                 names=True,

                 dtype=None,
                 delimiter=' ')
print(data)

最佳答案

您可以添加 invalid_raise = False 来跳过有问题的行或 usecols=np.arange(0, 3),但是我会采用以下方法:

列表.txt:

1 9 1 6 "Thu Feb 13 13:12:30 2014     "
0 0 0 0 0 0
38 38 915 915
"CJE                                                                              "
"2                                      "
"110321-025-01D-1ST                    
0 0 1 .1 73.7972 17 50
1 0 7 1 60 0 0 0 0
0 "                           "
1 0
#
38 38 No Data
39 38 No Data
40 38 No Data
41 38 3
42 38 No Data
43 38 4
44 38 4
45 38 5

然后:

logFile = "list.txt"

# opening the file
with open(logFile) as f:

    #reading the lines after slicing it i.e. 11
    content = f.readlines()[11:]

# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]

# for each line in content
for line in content:

     # if the line has No Data in it
     if line.find("No Data"):

         # Replacing the No Data with 0 using replace() method
         line = line.replace("No Data", "0")
     print(line)

输出:

38 38 0
39 38 0
40 38 0
41 38 3
42 38 0
43 38 4
44 38 4
45 38 5

编辑:

将它们添加到 3 列矩阵中:

_list = []
# for each line in content
for line in content:

     # if the line has No Data in it
     if line.find("No Data"):

         # Replacing the No Data with 0 using replace() method
         line = line.replace("No Data", "0")
     # print(line)
     # list comprehension for splitting on the basis of space and appending to the list
     _list.append([e for e in line.split(' ') if e])

print(_list)

输出:

[['38', '38', '0'], ['39', '38', '0'], ['40', '38', '0'], ['41', '38', '3'],
 ['42', '38', '0'], ['43', '38', '4'], ['44', '38', '4'], ['45', '38', '5']]

编辑2:

要删除文件中的最后一行,您可以使用切片 content[:-1]::

logFile = "list.txt"

# opening the file
with open(logFile) as f:

    #reading the lines after slicing it i.e. 11
    content = f.readlines()[11:]

_list = []
# for each line in content
for line in content[:-1]:

     # if the line has No Data in it
     if line.find("No Data"):
         # Replacing the No Data with 0 using replace() method
         line = line.replace("No Data", "0")
     # list comprehension for splitting on the basis of space and appending to the list
     _list.append([e for e in line.strip().split(' ') if e])


print(_list)

输出:

[['38', '38', '0'], ['39', '38', '0'], ['40', '38', '0'], ['41', '38', '3'],
 ['42', '38', '0'], ['43', '38', '4'], ['44', '38', '4'], ['45', '38', '5']]

关于python - 导入函数参数 numpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54553763/

相关文章:

python - 如何使用 genfromtxt() 从 NumPy 中的文本文件中读取不同长度的列?

python - 使用 Python 从目录中读取所有 csv 文件

python - 在 Python 中,使用 `del` 语句是代码异味吗?

python - 使用 Spectrify 将数据以 Parquet 格式从 Redshift 卸载到 S3

python - 两个巨大稠密矩阵的乘法 Hadamard-乘以一个稀疏矩阵

python - 使用 Matplotlib 注释正态分布图中的四分位数

python - 错误 "AttributeError: ' unicode' object has no attribute 'read' "on file upload

python - 聚合行并创建列作为值的计数

python - numpy.dot 速度很慢,但安装了 blas 和 lapack,如何解决?

python - Genfromtxt 抛出异常 "got 3 columns instead of 27"但事实并非如此