Skip to main content
 首页 » 编程设计

Python:将输入文件中行的最后一个单词 append 到输出文件行的末尾

2024年11月01日24三少

我有每日温度文件,我想将其合并为一个年度文件。
例如输入文件

 Day_1.dat               
 Toronto  -22.5      
 Montreal -10.6   
 
 Day_2.dat             
 Toronto  -15.5   
 Montreal  -1.5   
 
 Day_3.dat       
 Toronto   -5.5 
 Montreal  10.6   

想要的输出文件

Toronto  -22.5 -15.5 -5.5   
Montreal -10.6  -1.5 10.6 

到目前为止,这是我为程序的这一部分编写的代码:

    #Open files for reading (input) and appending (output) 
    readFileObj = gzip.open(readfilename, 'r') #call built in utility to unzip file for    reading 
    appFileObj = open(outFileName, 'a') 
      for line in readfileobj: 
        fileString = readFileObj.read(line.split()[-1]+'\n') # read last 'word' of each line 
        outval = "" + str(float(filestring) +"\n" #buffer with a space and then signal end of line 
        appFileObj.write(outval) #this is where I need formatting help to append outval 

请您参考如下方法:

这里迭代fileinput.input允许我们遍历所有文件,一次获取一行。现在我们在空白处拆分每一行,然后使用城市名称作为键,我们将相应的温度(或任何值)存储在列表中。

import fileinput 
d = {} 
for line in fileinput.input(['Day_1.dat', 'Day_2.dat', 'Day_3.dat']): 
    city, temp = line.split() 
    d.setdefault(city, []).append(temp) 

现在 d 包含:

{'Toronto': ['-22.5', '-15.5', '-5.5'], 
 'Montreal': ['-10.6', '-1.5', '10.6']} 

现在,我们可以简单地迭代这个字典并将数据写入输出文件。

with open('output_file', 'w') as f: 
    for city, values in d.items(): 
       f.write('{} {}\n'.format(city, ' '.join(values))) 

输出:

$ cat output_file  
Toronto -22.5 -15.5 -5.5 
Montreal -10.6 -1.5 10.6 

请注意,字典没有任何特定的顺序。因此,此处的输出可能首先是 Montreal,然后是 Toronto。如果顺序很重要,那么您需要使用 collections.OrderedDict。


代码的工作版本:

d = {} 
#Considering you've a list of all `gzip` files to be opened. 
for readfilename in filenames:   
    #populate the dictionary by collecting data from each file 
    with gzip.open(readfilename, 'r') as f: 
        for line in f: 
            city, temp = line.split() 
            d.setdefault(city, []).append(temp) 
 
#Now write to the output file 
with open(outFileName, 'w') as f: 
    for city, values in d.items(): 
       f.write('{} {}\n'.format(city, ' '.join(values)))