简体   繁体   English

如何在for循环之外永久+1变量

[英]How to permanently +1 a variable outside of a for loop

I have a Python script that pulls cryptocurrency data from Yahoo Finance.我有一个 Python 脚本,它从雅虎财经提取加密货币数据。 It scrapes this data and then inputs it into a Google Sheet.它会抓取这些数据,然后将其输入到 Google 表格中。 What I want is for this script to run once per day, which I will schedule through task scheduler, and each day pull the data from Yahoo Finance and then input into the sheet.我想要的是这个脚本每天运行一次,我将通过任务调度程序安排,每天从雅虎财经中提取数据,然后输入到工作表中。

I want this spreadsheet to store data historically.我希望这个电子表格历史地存储数据。 For example, first column = today's data, second column = tomorrow's data (which the script will collect when it runs tomorrow) which means the column value needs to +1 after the script runs every time.例如,第一列 = 今天的数据,第二列 = 明天的数据(脚本将在明天运行时收集),这意味着每次脚本运行后列值需要 +1。 I've put the column number into a variable, so what I'm essentially trying to do is this;我已经将列号放入一个变量中,所以我实际上想要做的是这个;

today, c tomorrow, c + 1 day after, c + 2今天,c 明天,c + 1 天后,c + 2

and so on, so that the sheet gets filled out and the data doesn't get overwritten.依此类推,以便填写工作表并且数据不会被覆盖。 I doubt there's a way to permanently change the value of the variable other than going into the script and changing the value of c manually before it runs.我怀疑除了进入脚本并在运行之前手动更改 c 的值之外,还有其他方法可以永久更改变量的值。 Is there any other way to go about this? go 还有其他方法吗?

Not sure how to tackle this - c = c + 1 at the end of the function was my best guess but I know this won't work.不知道如何解决这个问题 - c = c + 1 在 function 的末尾是我最好的猜测,但我知道这不会工作。 The only other thing I can think of is to write out this function hundreds of times, first time c = 1, second time c = 2 and so on with "time.sleep(86400)" in between functions and have the script running constantly but as you can imagine I'd rather not do that. The only other thing I can think of is to write out this function hundreds of times, first time c = 1, second time c = 2 and so on with "time.sleep(86400)" in between functions and have the script running constantly但正如你可以想象的那样,我宁愿不这样做。

def google_sheet_import(var1):
    r = 2
    c = 3
    for w in (var1):
        cc_worksheet.update_cell(r, c, w.text)
        r = r + 1

    google_sheet_import(intraday_price)

All that happens is the script overwrites the data in c because I can't get it to permanently change the value of c after the script runs.所发生的只是脚本覆盖 c 中的数据,因为我无法让它在脚本运行后永久更改 c 的值。

In order to accomplish your needs, you have many possible options to follow:为了满足您的需求,您有许多可能的选择:

  1. Store the c value somewhere in your sheet.c值存储在工作表中的某个位置。 After inserting a new column, you should increment it, and before inserting a new one you should read it and set it to be c 's value.插入新列后,您应该增加它,在插入新列之前,您应该阅读它并将其设置为c的值。
  2. Store the c variable in a file (see python's open function) and increment it accordingly.c变量存储在一个文件中(参见 python 的open函数)并相应地递增它。
  3. The one that is probably the most sensible and the one I will provide code for.一个可能是最明智的,也是我将为其提供代码的那个。 Compute the number of columns you have in your sheet when the python script is executed, and then assign c 's value based on that:计算执行 python 脚本时工作表中的列数,然后根据该值分配c的值:
def google_sheet_import(var1):
    r = 2
    c = len(cc_worksheet.get_all_values()[0])
    for w in (var1):
        cc_worksheet.update_cell(r, c, w.text)
        r = r + 1

    google_sheet_import(intraday_price)

The only change is in the 3d line.唯一的变化是 3d 系列。 What it does is to check which is the last cell that is populated within the first row (in case your first row is empty or can be empty, I recommend that you set it to another row that you know will be populated).它的作用是检查第一行中填充的最后一个单元格(如果您的第一行为空或可以为空,我建议您将其设置为您知道将填充的另一行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM