如何拆分为列

5 投票
3 回答
5173 浏览
提问于 2025-04-28 18:24

我有一个文件,里面有两个数据集,我想把它们读入Python,作为两列。

数据的格式是:

xxx yyy    xxx yyy   xxx yyy

等等,所以我明白我需要以某种方式把它们分开。我刚开始学Python(而且编程也还不太熟练),所以到目前为止我有点挣扎。目前我尝试使用:

def read(file):

    column1=[]
    column2=[]
    readfile = open(file, 'r')
    a = (readfile.read())
    readfile.close()

我该如何把读取的文件分成第一列和第二列呢?

暂无标签

3 个回答

-2

在你的例子中,数据集的第二次分隔是用三个空格来分开的……所以我觉得数据集的分隔至少是用两个空格……

#reading a file seems not to be your problem ;)
#works also with more than 3/4/n spaces...
data = 'xxx yyy    xxx yyy             xxx yyy'

#reduce more than two spaces
while '   ' in data:
    data = data.replace('   ', '  ')

#split data-sets who are now separated trough two spaces
data = data.split('  ')

#split into cols for each data-set
data = [x.split(' ') for x in data]

#reshape for better (requested?) access
column1, column2 = zip(*data)

print column1
print column2

输出是:

('xxx', 'xxx', 'xxx')
('yyy', 'yyy', 'yyy')

希望这对你有帮助 :)

2

这在使用Python的Pandas模块时非常简单。假设你有一个这样的数据文件:

>cat data.txt
xxx  yyy  xxx  yyy  xxx yyy
xxx yyy    xxx yyy   xxx yyy
xxx yyy  xxx yyy   xxx yyy
xxx yyy    xxx yyy  xxx yyy
xxx yyy    xxx  yyy   xxx yyy

>from pandas import DataFrame
>from pandas import read_csv
>from pandas import concat
>dfin = read_csv("data.txt", header=None, prefix='X', delimiter=r"\s+")
> dfin
X0   X1   X2   X3   X4   X5
0  xxx  yyy  xxx  yyy  xxx  yyy
1  xxx  yyy  xxx  yyy  xxx  yyy
2  xxx  yyy  xxx  yyy  xxx  yyy
3  xxx  yyy  xxx  yyy  xxx  yyy
4  xxx  yyy  xxx  yyy  xxx  yyy
>dfout = DataFrame()
>dfout['X0'] = concat([dfin['X0'], dfin['X2'], dfin['X4']], axis=0, ignore_index=True)
>dfout['X1'] = concat([dfin['X1'], dfin['X3'], dfin['X5']], axis=0, ignore_index=True)
> dfout
 X0   X1
 0   xxx  yyy
 1   xxx  yyy
 2   xxx  yyy
 3   xxx  yyy
 4   xxx  yyy
 5   xxx  yyy
 6   xxx  yyy
 7   xxx  yyy
 8   xxx  yyy
 9   xxx  yyy
 10  xxx  yyy
 11  xxx  yyy
 12  xxx  yyy
 13  xxx  yyy
 14  xxx  yyy

希望这对你有帮助。祝好。

0

这是一个简单的例子,教你如何获取第一列的xxx值和第二列的yyy值。

重要提示!你的文件数据格式应该是这样的:

xxx yyy    xxx yyy    xxx yyy
每组数据(xxx yyy    xxx yyy)之间要有4个空格,每对数据(xxx yyy)之间要有1个空格


你也可以使用其他分隔符,比如这样:

xxx,yyy/xxx,yyy/xxx,yyy   
这时你只需要把 data_separator=','column_separator='/' 改一下就行

或者

xxx-yyy/xxx-yyy/xxx-yyy   
这时你只需要把 data_separator='-'column_separator='/' 改一下就行

def read(file):
    column1=[]
    column2= []
    readfile = open(file, 'r')
    data_separator = ' '  # one space to separate xxx and yyy
    column_separator = '    '  # 4 spaces to separate groups xxx,yyy    xxx,yyy

    for line in readfile.readlines():  # In case you have more than 1 line
         line = line.rstrip('\n')  # Remove EOF from line
         print line

         columns = line.split(column_separator)  # Get the data groups 
         # columns now is an array like ['xxx yyy', 'xxx yyy', 'xxx yyy']

         for column in columns:
             if not column: continue  # If column is empty, ignore it
             column1.append(column.split(data_separator)[0])
             column2.append(column.split(data_separator)[1])
    readfile.close()

我有一个文本文件,里面的内容是 xxx yyy aaa bbb ttt hhh,调用这个函数后,结果是:

column1 = ['xxx', 'aaa', 'ttt']
column2 = ['yyy', 'bbb', 'hhh']

撰写回答