python中使用非统一行解析数据

python中使用非统一行解析数据,python,parsing,pandas,Python,Parsing,Pandas,我有一个数据集,我想分析它。我想拉出特定的列,然后在非统一行之前和之后将它们分开。下面是我的数据的一个例子:注意中间的三行与其他行的格式不匹配: 1386865618963 1 M subject_avatar 3.636229 1.000000 5.422941 30.200327 0.000000 0.000000 1386865618965 1 M subject_avatar 3.631835 1.000000 5.41

我有一个数据集,我想分析它。我想拉出特定的列,然后在非统一行之前和之后将它们分开。下面是我的数据的一个例子:注意中间的三行与其他行的格式不匹配:

1386865618963   1   M   subject_avatar  3.636229    1.000000    5.422941    30.200327   0.000000    0.000000
1386865618965   1   M   subject_avatar  3.631835    1.000000    5.415390    30.200327   0.000000    0.000000
1386865618966   2   M   subject_avatar  3.627432    1.000000    5.407826    30.200327   0.000000    0.000000
1386865618968   1   M   subject_avatar  3.625223    1.000000    5.404030    30.200327   0.000000    0.000000
1386865618970   1   M   subject_avatar  3.620788    1.000000    5.396411    30.200327   0.000000    0.000000
1386865618970   0   D   4345048336
1386865618970   0   D   4345763672
1386865618971   0   I   BOXGEOM (45.0, 0.0, -45.0, 19.0, 3.5, 19.0) {'callback': <bound method YCEnvironment.dropoff of <navigate.YCEnvironment instance at 0x103065440>>, 'cbargs': (0, {'width': 1.75, 'image': <pyepl.display.Image object at 0x102f9da90>, 'height': 4.75, 'volbitSize': (0.5, 0.71999999999999997), 'name': 'Julia'}, {'width': 0.69999999999999996, 'name': 'Flower Patch', 'realpos': (45.0, 0.0, -45.0), 'image': <pyepl.display.Image object at 0x102fc3f50>, 'realsize': (7.0, 3.5, 7.0), 'type': 'store', 'volbitSize': (0.5, 0.5), 'height': 0.34999999999999998}), 'permiable': True}  4926595152
1386865618972   1   M   subject_avatar  3.621182    1.000000    5.396492    30.200327   0.000000    0.000000
1386865618992   2   M   subject_avatar  3.621182    1.000000    5.396492    30.200327   0.000000    0.000000
1386865618996   1   M   subject_avatar  3.621182    1.000000    5.396492    30.200327   0.000000    0.000000
1386865618998   2   M   subject_avatar  3.621182    1.000000    5.396492    30.200327   0.000000    0.000000
1386865619002   1   M   subject_avatar  3.621182    1.000000    5.396492    30.200327   0.000000    0.000000
1386865619005   1   M   subject_avatar  3.621182    1.000000    5.396492    30.200327   0.000000    0.000000
1386865619008   1   M   subject_avatar  3.621182    1.000000    5.396492    30.200327   0.000000    0.000000
138686618963 1 M主题_阿凡达3.636229 1.000000 5.422941 30.200327 0.000000 0.000000
138686618965 1 M主题_阿凡达3.631835 1.000000 5.415390 30.200327 0.000000 0.000000
1386861618966 200米主题_阿凡达3.627432 1.000000 5.407826 30.200327 0.000000 0.000000
138686618968 1M主题_阿凡达3.625223 1.000000 5.404030 30.200327 0.000000 0.000000
138686618970 1M主题_阿凡达3.620788 1.000000 5.396411 30.200327 0.000000 0.000000
1386865618970 0 D 4345048336
138686618970 0 D 4345763672
1386861618971 0 I BOXGEOM(45.0,0.0,-45.0,19.0,3.5,19.0){'callback':,'cbargs':(0,{'width':1.75,'image':,'height':4.75,'volbitSize':(0.5,0.71999999999999997),'name':'Julia'},{'width':0.69999999996,'name':'flowerpatch','realpos':(45.0,0.0.0,'image':'realsize,','type':'store','volbitSize':(0.5,0.5),'height':0.3499999999998}),'permisable':True}4926595152
138686618972 1M主题_阿凡达3.621182 1.000000 5.396492 30.200327 0.000000 0.000000
138686618992 2M主题_阿凡达3.621182 1.000000 5.396492 30.200327 0.000000 0.000000
138686618996 1M主题_阿凡达3.621182 1.000000 5.396492 30.200327 0.000000 0.000000
138686618998 2M主题_阿凡达3.621182 1.000000 5.396492 30.200327 0.000000 0.000000
138686561902 1米人物头像3.621182 1.000000 5.396492 30.200327 0.000000 0.000000
138686561905 1M主题_阿凡达3.621182 1.000000 5.396492 30.200327 0.000000 0.000000
138686561908 1米人物头像3.621182 1.000000 5.396492 30.200327 0.000000 0.000000
我之前问过一个问题()来将这些数据解析为列,但是,这些列只显示列中的项目数,而不显示项目本身


我意识到这是两个不同的问题(分成列,在非统一行之前和之后分开),但是如果您能在解析方面提供任何帮助,我将不胜感激

如果要省略非均匀线,只需检查每行的长度:

rows = []
for line in lines:
    row = line.split()
    if len(row) == 10:
        rows.append(row)

直截了当的想法:

您可以预处理原始文件以跳过所有不相关的行,可能:

with open('raw.txt', 'r') as infile:
    f = infile.readlines()
    with open('filtered.txt', 'w') as outfile:
        for line in f:
            if 'subject_avatar' in line: # or other better rules
                outfile.write(line)
然后使用
pandas
或其他方法处理
filtered.txt
干净的数据


以open('d.txt','r')作为填充:
f=infle.readlines()
以open('filtered_part1.txt','w')作为输出文件:
对于范围内的i(len(f)):
直线=f[i]
如果行[16]=“0”:
i+=1
打破
输出文件。写入(行)
而f[i][16]=“0”:#跳过几行
i+=1
以open('filtered_part2.txt','w')作为输出文件:
而i

这里提供了丑陋但可行的分离。基本上是找到0并跳过行。

你说的“分开”是什么意思?您只是想删除D&I行,还是希望以某种方式对每个Ms群集进行分组?我想删除D和I行并对Ms群集,以显示在D和I行之前发生的Ms以及在D和I行之后发生的Ms。谢谢,这很有效!现在,你知道我如何区分之前的数据和省略行之后的数据了吗?@Julia很高兴它起作用了。您是否只有一个这样的特定数据文件,或者上面的一个只是一个示例?@Julia我能想到的一种方法是逐行检查原始文件的第二列或第三列(字符串的特定索引)。一旦你遇到要省略的行,你就知道它是第一部分的结束,第二部分的开始。这只是数据文件中的一小部分,它是20000行。你知道我如何在第二列中使用0在行上表示它在前面或后面吗?@Julia请参阅更新的答案。就像现在一样直截了当。
with open('d.txt', 'r') as infile:
    f = infile.readlines()
    with open('filtered_part1.txt', 'w') as outfile:
        for i in range(len(f)):
            line = f[i]
            if line[16] == '0':
                i += 1
                break
            outfile.write(line)
    while f[i][16] == '0': # skip a few lines
        i += 1
    with open('filtered_part2.txt', 'w') as outfile:
        while i < len(f):
            outfile.write(f[i])
            i += 1