读入格式化行以填充数组

2024-06-16 09:43:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python将txt文件中一定数量的行读入数组。txt文件用FORTRAN格式化,有3个整数,两个3字符长,一个2字符长(即242388)。其中值为24238,8)。每行有7到3个整数“组”。下面是8行的txt文件。你知道吗

24238 8. 27237 8. 38 82 6. 38 96 6. 39 76 6. 39 77 6. 39 78 6.BARR  1
39 79 6. 39 80 6. 39 81 6. 39 82 6. 39 84 6. 39 85 6. 39 86 6.BARR  2
39 88 8. 39 89 8. 39 9010. 39 91 7. 39 92 7. 39 93 5. 39 96 6.BARR  3
39 9710. 39 9810. 39 9910. 3910010. 3910113. 3910212. 3910312.BARR  4
3910412. 3910512. 40 72 6. 40 73 6. 40 74 6. 40 75 6. 40 76 6.BARR  5
40 80 9. 40 8110. 40 8212. 40 8312. 40 84 8. 4010512. 4010612.BARR  6
40107 9. 40108 9. 40109 9. 41 70 6. 41 71 6. 41 77 6. 41 78 6.BARR  7
41 79 8. 41 80 8. 4110910. 41110 6. 41111 6. 41184 8. 42 73 2.BARR  8

我不想担心巴尔1等在每一行的结尾,也可以忽略它。我创建了一个初始值数组,希望用txt文件中的值填充该数组。你知道吗

import numpy as np
basin = open("il3_barrier","r")
zbm = np.full((171,251),-300)

我想要的是三个“组”中的第三个值根据第一个和第二个值填充数组。例如,我希望值8占据位置24、238的数组,以此类推。你知道吗

我正在使用stackoverflow上另一个答案的一段代码。但我不知道如何使用它来循环行。你知道吗

def slices(s, *args):
    position = 0
    for length in args:
        yield s[position:position + length]
        position += length

在这个例子中,我只看了8行,我想尝试这样的方法:

for h in range(0,8): 
    tempLine = basin.readline()
    for k in range(0,7):
        inw,jnw,hw = list(slices(tempLine,3,3,2))
        inw = int(inw)
        jnw = int(jnw)
        zbm [inw,jnw] = hw

它只返回每一行的第一组值,而不遍历整行。有没有办法让它在一行中循环遍历每组数字?或者有别的办法?你知道吗


Tags: 文件intxtfornpposition整数数组
3条回答

给你=^..^=

代码中的简短描述。你知道吗

import numpy as np

# load raw data
raw_data = []
with open('raw_data.txt', 'r') as file:
    data = file.readlines()
    for item in data:
        raw_data.append(item.strip())

# collect coordinates data
coordinates_data = []
for item in raw_data:
    for i in range(0, 63, 9):
        coordinates_data.append((item[0+i:2+i].strip(), item[2+i:5+i].strip(), item[6+i:7+i].strip().replace('.', '')))

# get values for array size
max_x = 0
max_y = 0
for item in coordinates_data:
    if max_x < int(item[0]):
        max_x = int(item[0])
    if max_y < int(item[1]):
        max_y = int(item[1])

# create empty array
final_array = np.zeros((max_x+1, max_y+1))

# load data into array
for item in coordinates_data:
    final_array[int(item[0]), int(item[1])] = int(item[2])

回答我自己的问题:

def slices7(s,pos, *args):
    S=[]
    for length in args:
        S.append(s[pos:pos + length])
        pos += length
    return pos,S

与…一起使用

for h in range(1,8):
    tempLine = basin.readline()
    pos=0
    for k in range(0,7):
        pos,AAA= list(slices7(tempLine,pos,3,3,3))
        try:
            inw = int(AAA[0])
            jnw = int(AAA[1])
            hw = float(AAA[2])
            zbm [inw,jnw] = hw
        except ValueError:
            pass

使用delimiter的字段宽度版本,我可以使用genfromtxt加载前两组数字(txt是文件示例的多行粘贴):

In [221]: dels = [2,3,3, 3,3,3]; cols=[0,1,2,3,4,5]                                                    
In [222]: np.genfromtxt(txt.splitlines(), delimiter=dels, usecols=cols, dtype=float)                   
Out[222]: 
array([[ 24., 238.,   8.,  27., 237.,   8.],
       [ 39.,  79.,   6.,  39.,  80.,   6.],
       [ 39.,  88.,   8.,  39.,  89.,   8.],
       [ 39.,  97.,  10.,  39.,  98.,  10.],
       [ 39., 104.,  12.,  39., 105.,  12.],
       [ 40.,  80.,   9.,  40.,  81.,  10.],
       [ 40., 107.,   9.,  40., 108.,   9.],
       [ 41.,  79.,   8.,  41.,  80.,   8.]])

因为'8'字段,我不得不使用dtypefloat。我想我可以缩短字段,跳过“.”。你知道吗

或者,如果我指定一个None类型,它将生成一个混合了整数和浮点数据类型字段的结构化数组。你知道吗

In [223]: np.genfromtxt(txt.splitlines(), delimiter=dels, usecols=cols, dtype=None)                    
Out[223]: 
array([(24, 238,  8., 27, 237,  8.), (39,  79,  6., 39,  80,  6.),
       (39,  88,  8., 39,  89,  8.), (39,  97, 10., 39,  98, 10.),
       (39, 104, 12., 39, 105, 12.), (40,  80,  9., 40,  81, 10.),
       (40, 107,  9., 40, 108,  9.), (41,  79,  8., 41,  80,  8.)],
      dtype=[('f0', '<i8'), ('f1', '<i8'), ('f2', '<f8'), ('f3', '<i8'), ('f4', '<i8'), ('f5', '<f8')])

delscols可以扩展以处理所有7个组。你知道吗

一旦你有了这样的数字,应该很容易把它们映射到你的最终坐标数组。但我想你已经解决了。你知道吗

===

将“.”拆分为自己的未使用列:

In [226]: dels = [2,3,2,1, 3,3,2,1]; cols=[0,1,2, 4,5,6]                                               
In [227]: np.genfromtxt(txt.splitlines(), delimiter=dels, usecols=cols, dtype=int)                     
Out[227]: 
array([[ 24, 238,   8,  27, 237,   8],
       [ 39,  79,   6,  39,  80,   6],
       [ 39,  88,   8,  39,  89,   8],
       [ 39,  97,  10,  39,  98,  10],
       [ 39, 104,  12,  39, 105,  12],
       [ 40,  80,   9,  40,  81,  10],
       [ 40, 107,   9,  40, 108,   9],
       [ 41,  79,   8,  41,  80,   8]])

相关问题 更多 >