【问题标题】:Python CSV File, Comparing Time PeriodPython CSV 文件,比较时间段
【发布时间】:2017-10-01 10:43:09
【问题描述】:

CSV 文件链接:https://www.emcsg.com/marketdata/priceinformation [我下载了 72 个周期的 CSV 文件。每天下午 12 点 - UTC+08:00,它有一个新文件,显示当天的价格和第二天上午 12 点之前的价格预测。]

我正在尝试显示能量 (USEP/$MWh) 低于每天平均值的日期和时间。

for line in lines:
    try:
        time = line.split(",")[1][1:-1]
        interval = line.split(",")[0][1:-1]
        item = line.split(",")[4][1:-1] #Choose 4th column and delete ""
        if interval == next_day:
          if float(item) < average:
              print interval, time, float(item)

    except:
        pass           #If it can't parse, the string is not a number

上面的代码打印出这样的东西

30 Sep 2017 01:00-01:30 84.14
30 Sep 2017 01:30-02:00 84.12
30 Sep 2017 02:00-02:30 85.11
30 Sep 2017 02:30-03:00 83.49
30 Sep 2017 03:00-03:30 80.66
30 Sep 2017 03:30-04:00 75.69
30 Sep 2017 04:00-04:30 72.45
         .
         .  
         .
30 Sep 2017 21:30-22:00 79.72
30 Sep 2017 22:00-22:30 73.23
30 Sep 2017 22:30-23:00 73.58
30 Sep 2017 23:00-23:30 72.14
30 Sep 2017 23:30-00:00 85.21

显示能源价格低于 9 月 30 日平均值的日期和时间。

但我想打印类似的东西

30 Sep 2017 01:00-04:30
30 Sep 2017 21:30-00:00

基本上我想将它们分组,因为时间是连续的。一旦出现中断(在此期间,价格高于平均水平),当价格低于平均水平时,它将在下一个“周期”打印一条新线。

我正在考虑将每个“期间”的结束时间(例如 01:00-01:30,01:30 是结束时间)与下一个期间的开始时间(例如 01:30-02:00 , 01:30 是开始时间)在下一行,但我不确定它是否可行。

提前谢谢!(:

【问题讨论】:

  • 不是 100% 清楚你要做什么
  • 我认为你做的是对的。一种方法:有两个变量,start 和 end.. 如果 end = start 如果不创建新条目,则追加到列表。

标签: python python-2.7 csv time


【解决方案1】:

这一定是长期以来我的最丑陋的代码之一。 但也许你的意思是这样的? 有些人认为这可能直接用 pandas 完成。

import pandas as pd

url = "https://www.emcsg.com/marketdata/priceinformation?downloadRealtime=true"
df = pd.read_csv(url)

average = df["USEP($/MWh)"].mean()
output = []
entry = 0
old = None

# Starts a loop 
# (if average changes from bigger to lower or vice versa 
# create new entry in the output list)
for k,v in df.iterrows():  

    # First entry
    if not old:
        output.append([])
        output[entry].append(v["Period"])
        if v["USEP($/MWh)"] > average:
            old = "bigger"
            output[0].append(old)
        else:
            old = "smaller"
            output[entry].append(old)
        output[entry].append(v["USEP($/MWh)"])
        continue

    # The rest
    if v["USEP($/MWh)"] > average:
        new = "bigger"
    else:
        new = "smaller"

    if new == old:
        output[entry][0] = output[entry][0].split("-")[0]+"-"+v["Period"].split("-")[1]
        output[entry][2] += v["USEP($/MWh)"]
    else:
        entry += 1
        output.append([])
        output[entry].append(v["Period"])
        output[entry].append(new)
        output[entry].append(v["USEP($/MWh)"])

    old = new

输出如下:

[['12:00-15:30', 'bigger', 503.52],
 ['15:30-18:30', 'smaller', 423.78],
 ['18:30-00:00', 'bigger', 839.39],
 ['00:00-10:00', 'smaller', 1372.4700000000003],
 ['10:00-11:30', 'bigger', 215.90999999999997],
 ['11:30-13:00', 'smaller', 211.83000000000004],
 ['13:00-17:00', 'bigger', 576.4200000000001],
 ['17:00-20:30', 'smaller', 486.94],
 ['20:30-22:00', 'bigger', 227.11],
 ['22:00-00:00', 'smaller', 271.34000000000003]]

【讨论】:

  • 好吧,谢谢你告诉我我的代码很丑。如果我知道如何很好地编码,而且我不只是处于必须学习如何编码的情况,我可能不会在这个网站上提问。不管怎样,谢谢你的帮助。
  • @Autumn 我永远不会告诉别人他/她的代码很丑,因为这里的人们寻求帮助,我认为自己是一个喜欢提供帮助的人。我在谈论我的代码。
  • @Autumn 如果他说你的代码很丑,那么你应该以积极的态度接受它,因为他是一个更有经验的程序员,他花时间回答你。 There is a community 其唯一目的是让人们将您的代码拆开。面对批评的消极攻击态度会让你在这个行业中一事无成。此外,无论您的技能水平如何,您都应该始终提出问题。总是。
  • @AntonvBR 好的,很抱歉造成误解。感谢您再次提供帮助。我很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 2021-02-16
  • 1970-01-01
相关资源
最近更新 更多