【问题标题】:Parsing Data with non-uniform rows in python在python中解析具有非均匀行的数据
【发布时间】:2014-01-24 02:40: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

我之前问了一个问题 (Parsing specific columns from a dataset in python) 来将这些数据解析成列,但是,这些列只显示列中的项目数,而不是项目本身。

我意识到这是两个不同的问题(分成列,在不统一的行之前和之后分开),但是任何有关解析的帮助都将不胜感激!

【问题讨论】:

  • “分离”是什么意思?您只是想删除 D & I 行,还是希望以某种方式对 Ms 的每个集群进行分组?
  • 我想删除 D 和 I 行并将 Ms 聚类以显示发生在 D 和 I 行之前的 Ms 以及发生在 D 和 I 行之后的 Ms。

标签: python parsing pandas


【解决方案1】:

如果你想省略不均匀的行,你可以简单地检查每一行的长度:

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

【讨论】:

    【解决方案2】:

    一个直截了当的想法:

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

    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 的干净数据。


    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
    

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

    【讨论】:

    • 谢谢,效果很好!现在,您知道我如何区分之前的数据和省略的行之后的数据吗?
    • @Julia 很高兴它成功了。您只有一个这样的特定数据文件还是上面的只是一个说明?
    • @Julia 我能想到的一种方法是逐行检查原始文件的第二列或第三列(字符串的特定索引)。一旦遇到要省略的那些行,您就知道这是第一部分的结尾和第二部分的开头。
    • 这只是数据文件中的一小部分,它是20,000行。你知道我如何在行上表示它在之前或之后,可能使用第二列中的 0 吗?
    • @Julia 查看更新的答案。就像它一样直截了当。
    猜你喜欢
    • 1970-01-01
    • 2011-07-16
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多