Numpy 如何使用空格分隔符从.txt文件中读取数据?

Numpy 如何使用空格分隔符从.txt文件中读取数据?,numpy,pandas,dataframe,Numpy,Pandas,Dataframe,您好,我有一个文件中的每日数据,每日数据按每月列排列。开始时包含一些文件信息。数据如下所示: Day Jan. Feb. Mar. Apr. May Jun. Jul. Aug. Sep. Oct. Nov. Dec. Year 01 15.2 12.3 9.96 10.1 15.0 33.7 137 309 182 62.6 27.4 17.2 02 14.9 1

您好,我有一个文件中的每日数据,每日数据按每月列排列。开始时包含一些文件信息。数据如下所示:

Day     Jan.   Feb.   Mar.   Apr.   May    Jun.   Jul.   Aug.   Sep.   Oct.   Nov.   Dec.   Year
 01     15.2   12.3   9.96   10.1   15.0   33.7    137    309    182   62.6   27.4   17.2
 02     14.9   12.3   9.96   9.96   16.4   38.2    109    342    197   69.9   25.4   16.6
 03     14.9   12.3   9.78   10.3   17.3   50.3    118    472    184   68.7   24.5   17.0
 04     14.6   12.3   9.69   10.3   18.7   58.1    152    275    190   68.7   24.5   16.6
 05     14.4   12.3   9.51   10.1   18.9   44.5    165    188    206   69.9   24.0   16.5
 06     14.1   12.3   9.41   10.3   19.8   44.8    142    157    192   62.2   23.8   16.1
 07     14.0   12.3   9.32   10.3   20.4   52.6    121    146    182   58.9   24.9   15.6
我使用此代码读取数据

data ='AQ404.7_01.txt'
with open(data) as fo:
    data = fo.readlines()[9:41]
    df = data[1:32]
    df = [d.strip() for d in df]
    df = (np.array(df))
    column = data[0][:-6]
    for string in (df):
        df = string.split()
        print df
但问题是,当我检查2月份的数据时,它给出了31个数据。我试图解决,但没能解决

有人能帮忙解决这个问题吗? 我也在这里附上了数据文件

您应使用熊猫读取器:

因此,对于输入文件,您已经定义了固定宽度列表:

#Define the column widths    
ws = [4,9,7,7,7,7,7,7,7,7,7,7,7]

#read the file having the header row in the 9th line and read only 31 lines after that
df = pd.read_fwf('AQ404.7_01.txt',widths=ws,header=9, nrows=31)

print df

我将您的样本复制粘贴到我的
ipython
会话中,作为多行文本,并运行以下
genfromtxt

In [281]: np.genfromtxt(txt.splitlines(),dtype=None,names=True,usecols=range(13))
Out[281]: 
array([(1, 15.2, 12.3, 9.96, 10.1, 15.0, 33.7, 137, 309, 182, 62.6, 27.4, 17.2),
       (2, 14.9, 12.3, 9.96, 9.96, 16.4, 38.2, 109, 342, 197, 69.9, 25.4, 16.6),
       (3, 14.9, 12.3, 9.78, 10.3, 17.3, 50.3, 118, 472, 184, 68.7, 24.5, 17.0),
       (4, 14.6, 12.3, 9.69, 10.3, 18.7, 58.1, 152, 275, 190, 68.7, 24.5, 16.6),
       (5, 14.4, 12.3, 9.51, 10.1, 18.9, 44.5, 165, 188, 206, 69.9, 24.0, 16.5),
       (6, 14.1, 12.3, 9.41, 10.3, 19.8, 44.8, 142, 157, 192, 62.2, 23.8, 16.1),
       (7, 14.0, 12.3, 9.32, 10.3, 20.4, 52.6, 121, 146, 182, 58.9, 24.9, 15.6)], 
      dtype=[('Day', '<i4'), ('Jan', '<f8'), ('Feb', '<f8'), ('Mar', '<f8'), ('Apr', '<f8'), ('May', '<f8'), ('Jun', '<f8'), ('Jul', '<i4'), ('Aug', '<i4'), ('Sep', '<i4'), ('Oct', '<f8'), ('Nov', '<f8'), ('Dec', '<f8')])

从scatch开始,我可以将这些行转换为一个列表,其中包含:

ll = []
for line in txt.splitlines():
    ll.append(line.strip().split())
我可以通过以下方式获得浮动列表:

for line in txt.splitlines()[1:]:   # skip the header
    ll.append([float(i) for i in line.strip().split()])
可以通过以下方式将其转换为二维阵列:

np.array(ll)

如果空白分隔符不起作用,
genfromtxt
也接受字段宽度列表作为“分隔符”。查看它的文档或实验。

在你得到一个很好的字符串和/或浮点列表之前,不要使用
np.array
。@hpaulj问题仍然是一样的:(你也可以在
np.genfromtxt
中指定字段宽度。@Abbas非常感谢你。你的代码非常有用和简单:)。
np.array(ll)