Python 将文本文件中的数据转换为数据帧

Python 将文本文件中的数据转换为数据帧,python,pandas,text,Python,Pandas,Text,我很难想出一个简单的解决方案,从以下格式的文本中创建一个简单的数据框架: Dose [Gy] Relative dose [%] Structure Volume [cm³] 0 0 45888.7 0.1 0.166667 27061.7 0.2 0.333333

我很难想出一个简单的解决方案,从以下格式的文本中创建一个简单的数据框架:

    Dose [Gy]   Relative dose [%]    Structure Volume [cm³]
            0                   0                   45888.7
          0.1            0.166667                   27061.7
          0.2            0.333333                   18911.6
          0.3                 0.5                   14907.6
          0.4            0.666667                   12602.7
          0.5            0.833333                   11127.8
          0.6                   1                   10041.9
          0.7             1.16667                   9184.75
          0.8             1.33333                   8480.96
          0.9                 1.5                   7885.19
            1             1.66667                   7382.82
          1.1             1.83333                   6947.77
          1.2                   2                   6570.69
          1.3             2.16667                   6242.93
          1.4             2.33333                   5959.37
          1.5                 2.5                   5713.12
          1.6             2.66667                   5497.12
          1.7             2.83333                   5305.86
          1.8                   3                    5135.8
          1.9             3.16667                   4983.65
            2             3.33333                   4846.38
          2.1                 3.5                    4720.5
          2.2             3.66667                   4604.54
          2.3             3.83333                    4496.7
          2.4                   4                   4396.11
          2.5             4.16667                   4303.21
我所做的是直接索引每行上的值,如:

  for line in lines:
       value1 = line[10:20]
       value3 = line[55:70]
然而,它不是很像蟒蛇,而且一点也不健壮

现在,我正试图让熊猫做繁重的工作,并努力让数据正确地显示出来。例如:

df = pd.read_csv(StringIO.StringIO(data), sep="          ",engine='python')
它输出的内容仍然包括新行“\n”和“'”以及数字

有没有更聪明的方法来解决这个问题?或者我需要做大量的预处理,熊猫才能使用它

谢谢你的帮助/建议

使用固定宽度的文件,并将列位置作为元组对列表传递:

In [63]:
t="""    Dose [Gy]   Relative dose [%]    Structure Volume [cm³]
            0                   0                   45888.7
          0.1            0.166667                   27061.7
          0.2            0.333333                   18911.6
          0.3                 0.5                   14907.6
          0.4            0.666667                   12602.7
          0.5            0.833333                   11127.8
          0.6                   1                   10041.9
          0.7             1.16667                   9184.75
          0.8             1.33333                   8480.96
          0.9                 1.5                   7885.19
            1             1.66667                   7382.82
          1.1             1.83333                   6947.77
          1.2                   2                   6570.69
          1.3             2.16667                   6242.93
          1.4             2.33333                   5959.37
          1.5                 2.5                   5713.12
          1.6             2.66667                   5497.12
          1.7             2.83333                   5305.86
          1.8                   3                    5135.8
          1.9             3.16667                   4983.65
            2             3.33333                   4846.38
          2.1                 3.5                    4720.5
          2.2             3.66667                   4604.54
          2.3             3.83333                    4496.7
          2.4                   4                   4396.11
          2.5             4.16667                   4303.21"""
您可以看到最终df的格式正确:

df = pd.read_fwf(io.StringIO(t), colspecs=[(0,13),(14,33),(34,59)])
df

Out[63]:
    Dose [Gy]  Relative dose [%]  Structure Volume [cm³]
0         0.0           0.000000                45888.70
1         0.1           0.166667                27061.70
2         0.2           0.333333                18911.60
3         0.3           0.500000                14907.60
4         0.4           0.666667                12602.70
5         0.5           0.833333                11127.80
6         0.6           1.000000                10041.90
7         0.7           1.166670                 9184.75
8         0.8           1.333330                 8480.96
9         0.9           1.500000                 7885.19
10        1.0           1.666670                 7382.82
11        1.1           1.833330                 6947.77
12        1.2           2.000000                 6570.69
13        1.3           2.166670                 6242.93
14        1.4           2.333330                 5959.37
15        1.5           2.500000                 5713.12
16        1.6           2.666670                 5497.12
17        1.7           2.833330                 5305.86
18        1.8           3.000000                 5135.80
19        1.9           3.166670                 4983.65
20        2.0           3.333330                 4846.38
21        2.1           3.500000                 4720.50
22        2.2           3.666670                 4604.54
23        2.3           3.833330                 4496.70
24        2.4           4.000000                 4396.11
25        2.5           4.166670                 4303.21

我认为您需要分隔符
s{2,}
-2或更多的空格:

import pandas as pd
import numpy as np
from pandas.compat import StringIO

temp=u"""Dose [Gy]   Relative dose [%]    Structure Volume [cm³]
            0                   0                   45888.7
          0.1            0.166667                   27061.7
          0.2            0.333333                   18911.6
          0.3                 0.5                   14907.6
          0.4            0.666667                   12602.7
          0.5            0.833333                   11127.8
          0.6                   1                   10041.9
          0.7             1.16667                   9184.75
          0.8             1.33333                   8480.96
          0.9                 1.5                   7885.19
            1             1.66667                   7382.82
          1.1             1.83333                   6947.77
          1.2                   2                   6570.69
          1.3             2.16667                   6242.93
          1.4             2.33333                   5959.37
          1.5                 2.5                   5713.12
          1.6             2.66667                   5497.12
          1.7             2.83333                   5305.86
          1.8                   3                    5135.8
          1.9             3.16667                   4983.65
            2             3.33333                   4846.38
          2.1                 3.5                    4720.5
          2.2             3.66667                   4604.54
          2.3             3.83333                    4496.7
          2.4                   4                   4396.11
          2.5             4.16667                   4303.21"""
#after testing replace StringIO(temp) to filename
df = pd.read_csv(StringIO(temp),sep=r'\s{2,}', engine='python')

尽管其他解决方案可能更像python,但我建议首先转换文件,使其不再包含多个空格。然后,您可以轻松地将其读入数据帧:

import pandas as pd

infile = open('test.txt', 'r')
outfile = open('testout.txt', 'w')

for eachrow in infile:
    stripped = '#'.join(filter(None,eachrow.split('  ')))
    outfile.write(stripped)

infile.close()
outfile.close()

df = pd.read_csv('testout.txt', encoding = 'latin1', sep='#', engine='python')
df.head()

你能不能按原样发布原始文本文件,而不是像你所做的那样以列表的形式发布,因为这会让事情变得混乱,谢谢,希望这就是你的意思?
import pandas as pd

infile = open('test.txt', 'r')
outfile = open('testout.txt', 'w')

for eachrow in infile:
    stripped = '#'.join(filter(None,eachrow.split('  ')))
    outfile.write(stripped)

infile.close()
outfile.close()

df = pd.read_csv('testout.txt', encoding = 'latin1', sep='#', engine='python')
df.head()