导入函数参数 numpy
importing function parameters numpy
我正在尝试导入一个文本文件 (.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
#
文本文件有一个header(前11行),其中包含一些数值,如下所示,数据也分为三列,其中一列,有数值,但也书写的字符:"No Data"。我还想将 "No Data" 更改为数值 0.
我可以跳过 Header,但我遇到的主要问题是告诉代码有三列,其中 "no data" 表示 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)
,但我会采用以下方法:
list.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']]
这是一种不同的方法。首先,读取所有行并将每一行放入列表的一个元素中。这都是由 readlines() 完成的。然后,忽略前 11 个句子。然后,对于行列表中的每一行,将 "No Data" 替换为 0。然后,将所有行粘合在一起以形成一个字符串。一个 numpy 数组由这个字符串组成,并重塑为正确的格式
import numpy as np
#Open the file and read the lines as a list of lines
with open('/home/we4sea/PycharmProjects/Noonreport-processing/GUI/test.txt','r') as f:
file = f.readlines()
#Skip the first 11 lines
file = file[11:]
#Create new list where the replaced lines are placed
replaced = []
#Replace "No Data" with 0
for line in file:
replaced.append(line.replace('No Data', '0'))
#Concatenate list to a single string
file = ''.join(replaced)
#Create numpy array from it and reshape to the correct format
data = np.fromstring(file, sep=' ').reshape(-1,3)
#Print the data
print(data)
输出:
[[38. 38. 0.]
[39. 38. 0.]
[40. 38. 0.]
[41. 38. 3.]
[42. 38. 0.]
[43. 38. 4.]
[44. 38. 4.]
[45. 38. 5.]]
我正在尝试导入一个文本文件 (.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
#
文本文件有一个header(前11行),其中包含一些数值,如下所示,数据也分为三列,其中一列,有数值,但也书写的字符:"No Data"。我还想将 "No Data" 更改为数值 0.
我可以跳过 Header,但我遇到的主要问题是告诉代码有三列,其中 "no data" 表示 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)
,但我会采用以下方法:
list.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']]
这是一种不同的方法。首先,读取所有行并将每一行放入列表的一个元素中。这都是由 readlines() 完成的。然后,忽略前 11 个句子。然后,对于行列表中的每一行,将 "No Data" 替换为 0。然后,将所有行粘合在一起以形成一个字符串。一个 numpy 数组由这个字符串组成,并重塑为正确的格式
import numpy as np
#Open the file and read the lines as a list of lines
with open('/home/we4sea/PycharmProjects/Noonreport-processing/GUI/test.txt','r') as f:
file = f.readlines()
#Skip the first 11 lines
file = file[11:]
#Create new list where the replaced lines are placed
replaced = []
#Replace "No Data" with 0
for line in file:
replaced.append(line.replace('No Data', '0'))
#Concatenate list to a single string
file = ''.join(replaced)
#Create numpy array from it and reshape to the correct format
data = np.fromstring(file, sep=' ').reshape(-1,3)
#Print the data
print(data)
输出:
[[38. 38. 0.]
[39. 38. 0.]
[40. 38. 0.]
[41. 38. 3.]
[42. 38. 0.]
[43. 38. 4.]
[44. 38. 4.]
[45. 38. 5.]]