3

我得到一个程序的输出数据文件,它看起来像这样,每个时间步都有不止一行:

0.0000E+00   0.0000E+00   0.0000E+00   0.0000E+00   0.0000E+00   0.0000E+00 \n   0.0000E+00   0.0000E+00   0.0000E+00   0.0000E+00
7.9819E-06   1.7724E-02   2.3383E-02   3.0048E-02   3.8603E-02   4.9581E-02 \n  5.6635E-02   4.9991E-02   3.9052E-02   3.0399E-02
....

我想把它排成十列

我制作了一个 Python 脚本,使用正则表达式在正确的行中删除 \n,但我认为应该有一种更简单更优雅的方法来做到这一点,这是我的脚本:

import re

with open('inputfile', encoding='utf-8') as file1:
       datai=file1.read()

dataf=re.sub(r'(?P<nomb>(   \d\.\d\d\d\dE.\d\d){8})\n','\g<nomb>',datai)

with open('result.txt',mode='w',encoding='utf-8') as resultfile:
        resultfile.write(datof)
4

4 回答 4

2

你可以尝试一个简单的

single_list = []
with open(your_file) as f:
    for line in f.readlines():
        single_list.extend(line.rstrip().split())

list_of_rows = [single_list[i*10:i*10+10] for i in range(len(single_list)//10)]

with open(output_file) as f:
    for line in list_of_rows:
        f.write(' '.join(line) + '\n')

如果您的所有数据都可以读取为单个字符串(使用 your data = f.read()),您还可以:

merged_data = data.replace("\n", " ")
single_list = merged_data.split()

single_list如上所述使用。


如果输入文件很大并且创建临时列表存在内存问题,您可以尝试以下操作:

    with open(input_file,'r') as inpf, open(output_file,'w') as outf:
        writable = []
        for line in input_file:
            row = line.rstrip().split()
            writable.extend(row)
            while len(writable) >= 10:
                outf.write(" ".join(writable[:10]) + "\n")
                writable = writable[10:]
于 2012-09-27T08:31:02.057 回答
1

您可以split()在每一行(或一组留置权)上使用来生成一个字符串列表,每个字符串包含一个数字,用于<string>.join(<list_of_numbers>)将它们连接到一个新行中。

于 2012-09-27T08:32:18.363 回答
1

您可以创建一个字典来将数据存储在类似结构的列中:

with open('inputfile', encoding='utf-8') as file1:
      in_f=file1.readlines()
arr = [line.strip().split('   ') for line in in_f] # or is it a tab that separates the  values?
# create an empty dict
db = {}

# use the index of the elements as a key
for i in range(len(arr[0])):
    db[i]=[]

# loop through first through the lists, then 
# iterate over the elements... 
for line in arr:
    for i,element in enumerate(line):
        db[i].append(element)

输出:

>>> db {0: ['0.0000E+00', '7.9819E-06'], 1: ['0.0000E+00', '1.7724E-02'], 2: ['0.0000E+00','2.3383E-02'], 3: ['0.0000E+00', '3.0048E-02'], 4: ['0.0000E+00', '3.8603E-02'], 5: ['0.0000E+00', '4.9581E-02'], 6: ['0.0000E+00', '5.6635E-02'], 7: ['0.0000E+00', '4.9991E-02'], 8: ['0.0000E+00', '3.9052E-02'], 9: ['0.0000E+00', '3.0399E-02']}

于 2012-09-27T09:06:13.727 回答
1

我能想到的最简单的解决方案就是使用 numpy:

file = np.genfromtxt('file',unpack=True,names=True,dtype=None)

你得到的是一本你可以访问的字典

print file[1][1] 

或者如果您有标题,请使用这些:

print file['header']
于 2012-09-27T09:10:56.897 回答