如何创建Python函数,将7个每日文件中的值相加为一个每周文件?

2024-09-28 22:19:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个空格分隔的文件,其中包含不同纬度/经度位置的站点的每日降水量值。每日文件的格式如下:

纬度PRCP

22.0 110.4 1.2条

23.0 121.0 0.0

23.0 122.0 0.1

其中第一场等于纬度,第二场等于经度,第三场等于日总降水量。你知道吗

我期待着创建一个每周文件汇编从每一个使用相同格式的每周每日文件的总数。。。但我遇到了麻烦。对我来说,这可能更为棘手的是,每个每日文件可能没有所有的位置,这意味着行的数量可能会有所不同,我不能简单地将每个文件行中的TOTAL PRCP字段添加到每周文件中,因为它可能不是所有天都匹配的。你知道吗

我目前的方法是打开每个文件,迭代每一行,并将每个字段设置为一个变量,然后与第二个每日文件的变量进行比较,如果LAT和LON字段匹配,则用两个降水值之和写一行。。。然后将每天的数据与第二天的数据进行比较,并编写一个“sum”文件。你知道吗

   with open(sundayFile, "r") as sundayFile:
    with open(mondayFile, "r") as mondayFile:
        with open(addMex1, "a") as addFile:

            print "\n\nNow checking Sunday File: " + str(sundayFile) + " and Monday File: " + str(mondayFile) + "\n\n"

            for lineA in sundayFile:
                parsedLineA = lineA.split()
                LAT_A = parsedLineA[0]
                LON_A = parsedLineA[1]
                TOTAL_PRCP_A = parsedLineA[2]

                print "Line in Sunday File: " + LAT_A + "," + LON_A + "," + TOTAL_PRCP_A + "\n"

                for lineB in mondayFile:
                    parsedLineB = lineB.split()
                    LAT_B = parsedLineB[0]
                    LON_B = parsedLineB[1]
                    TOTAL_PRCP_B = parsedLineB[2]

                    print "Line in Monday File: " + LAT_B + "," + LON_B + "," + TOTAL_PRCP_B + "\n"


                    if LAT_A == LAT_B and LON_A == LON_B:
                        print "\n***** Found a match for station at longitude of " + LON_A + " and latitude of " + LAT_A + "\n"
                        LAT = LAT_A
                        LON = LON_A
                        TOTAL_PRCP = str(float(TOTAL_PRCP_A) + float(TOTAL_PRCP_B))

                        addFile.write(LAT + "," + LON + "," + TOTAL_PRCP + "\n")


                    else:
                        addFile.write(LAT_A + "," + LON_A + "," + TOTAL_PRCP_A + "\n")
                        addFile.write(LAT_B + "," + LON_B + "," + TOTAL_PRCP_B + "\n")

这是不是真的工作,我终于放弃了手动尝试在我的一端。。。必须有一个Python,优雅的方式来执行这一点。非常感谢您的帮助!你知道吗


Tags: 文件inwithfiletotallonprintlat
1条回答
网友
1楼 · 发布于 2024-09-28 22:19:11

defaultdict来计算降水量的累积和更简单。这个dict的键是按纬度和经度排序的。这样做的诀窍:

from collections import defaultdict

files = ['sunday.txt', 'monday.txt', 'tuesday.txt', 'wednesday.txt', 
         'thursday.txt', 'friday.txt', 'saturday.txt'
]

totals = defaultdict(float)

for fn in files:
    with open(fn) as f:
        for line in f.readlines():
            lat, long, prec = line.split()  # strings
            totals[(lat, long)] += float(prec)

# See what we have:
import pprint
pprint.pprint(totals)

以下是一些示例数据:

monday.txt
     
22.0 110.4 3.2
23.0 121.0 1.0
23.0 122.0 0.2
24.0 122.0 1.0

tuesday.txt
     -
22.0 110.4 1.0

wednesday.txt
      -
23.0 122.0 0.3

thursday.txt
      
24.0 122.0 1.0
25.0 1.0 1.0

friday.txt
     
24.0 122.0 1.1

saturday.txt
      
23.0 121.0 10.5

下面是以上代码的输出,以及这些文件:

{('22.0', '110.4'): 5.4,
 ('23.0', '121.0'): 11.5,
 ('23.0', '122.0'): 0.6000000000000001,
 ('24.0', '122.0'): 3.1,
 ('25.0', '1.0'): 1.0}

我没有采取额外的步骤将聚合数据写入相同格式的文件,我将把它作为练习;)

相关问题 更多 >