【问题标题】:How do I format this data?如何格式化这些数据?
【发布时间】:2015-02-01 17:38:24
【问题描述】:

我的文本文件目前有这种格式的数据:

------------NEW CALCULATION-----------
1.09532773E-02  9.93E-02
3.76554509E-03  9.93E-02
7.53080333E-03  9.93E-02
1.12954900E-02  9.93E-02
1.50593193E-02  9.92E-02
1.88220125E-02  9.92E-02
2.25832891E-02  9.92E-02
------------NEW CALCULATION-----------
9.71343145E-02  9.84E-02
0.100812949 9.84E-02
0.104485862 9.83E-02
0.108152986 9.83E-02
0.111814260 9.82E-02

所以每次新计算的开始都有一个小的“----NEW CALCULATION----”消息,然后数字分成两列。 目前,我使用 excel 将所有数字分成两列,但我找不到将每个计算分隔在自己的一组列中的方法。例如,我希望上面的数据看起来像(分成四列):

1.09532773E-02  9.93E-02  9.71343145E-02    9.84E-02
3.76554509E-03  9.93E-02  0.100812949   9.84E-02
7.53080333E-03  9.93E-02  0.104485862   9.83E-02
1.12954900E-02  9.93E-02  0.108152986   9.83E-02
1.50593193E-02  9.92E-02  0.111814260   9.82E-02
1.88220125E-02  9.92E-02
2.25832891E-02  9.92E-02

请注意,两列的数据点数不同。

请有人帮忙!

【问题讨论】:

    标签: excel text formatting


    【解决方案1】:

    异常快速和肮脏的python脚本:

    import itertools
    
    so = "\t" # separator for output
    infile = "infile" # file input
    outfile = "outfile" # file output
    
    s = open(infile).read()
    sep = "------------NEW CALCULATION-----------\n"
    s = [i.strip().split('\n') for i in s.strip(sep).split(sep)]
    s = [[j.strip().split() for j in i] for i in s]
    s = list(map(list,list(itertools.zip_longest(*s,fillvalue=['\t\t\t']))))
    o = open("outfile","w")
    for i in [so.join([i for j in s2 for i in j]) for s2 in s]:
        o.write(i+"\n")
    

    【讨论】:

    • OP 的问题在哪里提到了 Python 平台?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    • 2020-12-08
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    相关资源
    最近更新 更多