我有每日温度文件,我想合并成一个年度文件。
e、 g.输入文件
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
所需的输出文件
^{pr2}$这是目前为止我为程序的这一部分编写的代码:
#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
在这里,^{} 上的迭代允许我们迭代所有文件,每次取一行。现在我们在空白处分割每一行,然后使用城市名称作为键,在列表中存储相应的温度(或任何值)。在
现在
^{pr2}$d
包含:现在,我们可以简单地遍历这个字典并将数据写入输出文件。在
输出:
注意字典没有任何特定的顺序。因此,这里的输出可以是}。如果顺序很重要,则需要使用
Montreal
,然后是{collections.OrderedDict
。在代码的工作版本:
相关问题 更多 >
编程相关推荐