首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3 -更新内部循环中的“power”变量会修改外部循环的聚合变量

Python3 -更新内部循环中的“power”变量会修改外部循环的聚合变量
EN

Stack Overflow用户
提问于 2021-07-11 00:46:33
回答 1查看 39关注 0票数 0

我通读了一个目录中的所有文件(csv文件)-我忽略了每个文件的前7行“header”,然后找到了从第8行到第151行的时间和功率值。我读到的每一行都是示例行6-9和46-48的形式

代码语言:javascript
复制
Time;Power
HH:mm;kW
00:10;0.000
00:20;0.000
<snip>
06:20;0.012
06:30;0.042
06:40;0.060

我的代码成功地忽略了所有午夜后的零读数,并正确地识别了06:20的第一个非零功率读数。它使用时间(小时和分钟)来调整一个变量,该变量包含当天的时间戳,并将现在的小时/分钟包含的时间戳和功率存储在一个复杂的变量timePower[(timestamp),(power)]中。然后,它将变量timePower[(),()]附加到变量dayPower[]中,并再次从文件的下一行开始。

由于我不能计算出的原因,下一行解析正常,直到我用新的时间戳更新timePower[0],用新的幂更新timePower[1] -更新这些变量似乎也更新了dayPower[]变量(dayPower[0])中的现有条目,现在,就在我要追加一个新条目之前,旧的条目看起来与新的条目相同。新行成功追加。重复这种错误行为,直到我读取完所有非零值,并且dayPower[]中的所有行看起来都与最终条目相同。相关函数如下所示:

代码语言:javascript
复制
def parse_power_values(path, filename, theDate):
  timePower = [(),()] #a list that will be assigned two items; the timestamp and the power for that timeslot
  dayPower = [] # A list that will have all the timePowers for the day appended
  currentFile = open(path + filename,'r')
  for i, line in enumerate(currentFile):
    if i <= 7:
      doingSomething = True
      #print 'header' + str(i) + '/ ' + line.rstrip()
    elif ((i > 7) and (i <= 151)):
      lineParts = line.split(';')
      theTime = lineParts[0].split(':')
      theHour = theTime[0]
      theMin = theTime[1]
      timestamp = theDate.replace(hour=int(theHour),minute=int(theMin)) #theDate is a timestamp obj with the current date but hour & minute at 00:00 to start with.
      power = lineParts[1].rstrip()
      if power == '-.---':
        power = 0.000
      if (float(power) > 0):
        #append_to_database(con, timestamp,power)
        timePower[0] = timestamp
        timePower[1] = power
        dayPower.append(timePower)
    elif i > 151:
     return dayPower
     #break
  currentFile.close()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-12 06:34:06

@jasonharper是对的,我修改后的函数现在看起来像这样:

代码语言:javascript
复制
def parse_power_values(path, filename, theDate):
  # timePower = [(),()] #a list that will be assigned two items; the timestamp and the power for that timeslot
  dayPower = [] # A list that will have all the timePowers for the day appended
  currentFile = open(path + filename,'r')
  for i, line in enumerate(currentFile):
    if i <= 7:
      doingSomething = True
      #print 'header' + str(i) + '/ ' + line.rstrip()
    elif ((i > 7) and (i <= 151)):
      lineParts = line.split(';')
      theTime = lineParts[0].split(':')
      theHour = theTime[0]
      theMin = theTime[1]
      timestamp = theDate.replace(hour=int(theHour),minute=int(theMin))
      power = lineParts[1].rstrip()
      if power == '-.---':
        power = 0.000
      if (float(power) > 0):
        dayPower.append([timestamp,power])
    elif i > 151:
     return dayPower
     #break
  currentFile.close()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68329764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档