我得到的程序的输出数据文件看起来像这样,每个时间步都有多行:
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)
答案 0 :(得分: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')
如果您的所有数据都可以作为单个字符串读取(使用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:]
答案 1 :(得分:1)
您可以在每一行(或一组留言)上使用split()
生成一个包含每个数字的字符串列表,使用<string>.join(<list_of_numbers>)
将它们连接到一个新行。
答案 2 :(得分: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']}
答案 3 :(得分:1)
我能想到的最简单的解决方案就是使用numpy:
file = np.genfromtxt('file',unpack=True,names=True,dtype=None)
你得到的是你用
访问的字典print file[1][1]
或者如果您有标题,请使用:
print file['header']