在python中将序列连接成一个numpy数组

时间:2014-09-04 08:20:09

标签: python arrays numpy append concatenation

晚安,我有一个问题,我试图将字符串连接成一个数组,我有一个.txt,分为3 gruoups“.I 1”“。I 2”“。我3”我的代码生成在这种情况下,“.I n”总数的数组为3的数组,我的目标是将“.I x”之后的所有行存储在数组示例的x位置....“。I数组[1]中的1“,数组[2]中的.I 2”等。

我的代码只是在下一个“.I”之前保存最后一行而不是保存所有行,我知道这会发生但问题是我不知道如何连接或推送或追加行进入数组的相同位置

TXT DOCUMENT cran.all.txt

.I 1
.T
experimental investigation of the aerodynamics of a
wing in a slipstream .
.A
brenckman,m.
.B
j. ae. scs. 25, 1958, 324.
.W
experimental investigation of the aerodynamics of a
wing in a slipstream .
  an experimental study of a wing in a propeller slipstream was
made in order to determine the spanwise distribution of the lift
increase due to slipstream at different angles of attack of the wing
and at different free stream to slipstream velocity ratios .  the
results were intended in part as an evaluation basis for different
theoretical treatments of this problem .
  the comparative span loading curves, together with
supporting evidence, showed that a substantial part of the lift increment
produced by the slipstream was due to a /destalling/ or
boundary-layer-control effect .  the integrated remaining lift
increment, after subtracting this destalling lift, was found to agree
well with a potential flow theory .
  an empirical evaluation of the destalling effects was made for
the specific configuration of the experiment .


.I 2
.T
simple shear flow past a flat plate in an incompressible fluid of small
viscosity .
.A
ting-yili
.B
department of aeronautical engineering, rensselaer polytechnic
institute
troy, n.y.
.W
simple shear flow past a flat plate in an incompressible fluid of small
viscosity .
in the study of high-speed viscous flow past a two-dimensional body it
is usually necessary to consider a curved shock wave emitting from the
nose or leading edge of the body .  consequently, there exists an
inviscid rotational flow region between the shock wave and the boundary
layer .  such a situation arises, for instance, in the study of the
hypersonic viscous flow past a flat plate .  the situation is somewhat
different from prandtl's classical boundary-layer problem . in prandtl's
original problem the inviscid free stream outside the boundary layer is
irrotational while in a hypersonic boundary-layer problem the inviscid
free stream must be considered as rotational .  the possible effects of
vorticity have been recently discussed by ferri and libby .  in the
present paper, the simple shear flow past a flat plate in a fluid of small
viscosity is investigated .  it can be shown that this problem can again
be treated by the boundary-layer approximation, the only novel feature
being that the free stream has a constant vorticity .  the discussion
here is restricted to two-dimensional incompressible steady flow .


.I 3
.T
the boundary layer in simple shear flow past a flat plate .
.A
m. b. glauert
.B
department of mathematics, university of manchester, manchester,
england
.W
the boundary layer in simple shear flow past a flat plate .
the boundary-layer equations are presented for steady
incompressible flow with no pressure gradient .

PYTHON CODE

import re
import numpy as np

def total_archivos() :
    hand = open("cran.all.txt")
    total= 0
    for line in hand:
        line = line.strip()
        if ".I" in line:
            total=total+1
    return total

def escribir() :
    hand = open("cran.all.txt")
    total= 0
    for line in hand:
        line = line.strip()
        if ".I" in line:
            total=total+1
        else:
            textos[total-1]=np.array(line)



cuenta=total_archivos()
textos = np.array(range(cuenta), dtype='|S1000')
escribir()
print cuenta
print textos[0]
print textos[1]
print textos[2]

结果

1400
the specific configuration of the experiment .
here is restricted to two-dimensional incompressible steady flow .
incompressible flow with no pressure gradient .

请注意,这只是每个“I. x”的最后一行我想将所有行存储在同一个数组位置。

非常感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

我建议使用line.startswith("special_chr")line.endswith("special_chr")来处理句号结束和开始行!然后你有一个更强的 pythonic 代码,减少错误!

答案 1 :(得分:0)

我找到了一个更简单的解决方案,而不是担心数组中的连接,我创建了简单的时间变量,称为" doc"我使用这个变量连接行,然后当它们全部连接时我只是将变量保存在我想要的数组位置,然后我清理变量这就是我的代码结束的方式

import re
import numpy as np



def total_archivos() :
    hand = open("cran.all.txt")
    total= 0
    for line in hand:
        line = line.strip()
        if ".I" in line:
            total=total+1
    return total

def escribir(doc) :
    hand = open("cran.all.txt")
    total= 0
    for line in hand:
        line = line.strip()
        if ".I" in line:
            doc = ""
            total=total+1
        else:
            doc = doc + line
            textos[total-1]=doc



doc = ""
cuenta=total_archivos()
textos = np.array(range(cuenta), dtype='|S2000')
np.array(textos).tolist()
escribir(doc)
print cuenta
print textos[1330]