如何在Python中使用while循环迭代的所有值创建一个Excel文件?

huangapple go评论121阅读模式
英文:

How to create an excel file with all values of an while loop iteration in python?

问题

  1. 我想要以下代码将所有的输出保存在一个Excel文件中但每次我尝试运行代码时只保存最后一次迭代的结果但是当我在空闲状态下运行它时得到第二个结果
  2. ```python
  3. import urllib.request
  4. import datetime as dt
  5. from datetime import datetime, timezone, tzinfo
  6. import cdflib
  7. import numpy as np
  8. import pandas as pd
  9. from cdflib.epochs import CDFepoch
  10. import pathlib
  11. from xlsxwriter import Workbook
  12. from openpyxl import Workbook
  13. result = []
  14. months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
  15. days = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
  16. for year in range(2014, 2019, 1):
  17. for month in range(len(months)):
  18. for day in range(len(days)):
  19. def get_day_data(station: str, year, month, day):
  20. is_pg = station.startswith("pg")
  21. if is_pg:
  22. url = f"http://mist.nianet.org/CDFdata/{station}/{year}/thg_l2_mag_{station}_{year}{month}{day}_v01.cdf"
  23. else:
  24. url = f"https://ftp.space.dtu.dk/data/Ground_magnetometers/Adjusted/{station.upper()}/{year}/{month}/{station.upper()}_{year}{month}{day}.cdf"
  25. file_name = url.split("/")[-1]
  26. if not pathlib.Path(file_name).exists():
  27. try:
  28. urllib.request.urlretrieve(url, "tmp.cdf")
  29. pathlib.Path("tmp.cdf").rename(file_name)
  30. except:
  31. raise Exception(f"Failed to download {url}")
  32. cdf = cdflib.CDF(file_name)
  33. if is_pg:
  34. epoch = cdf.varget(f"thg_mag_{station}_time")
  35. index = pd.to_datetime(epoch, utc=False, unit="s")
  36. #index = index.round("1s")
  37. variable = f"thg_mag_{station}"
  38. else:
  39. epoch = cdf.varget("time")
  40. epoch = CDFepoch().to_datetime(epoch, to_np=True)
  41. index = pd.to_datetime(epoch)
  42. variable = "HEZ"
  43. values = cdf.varget(variable)
  44. if "FILLVAL" in cdf.varattsget(variable):
  45. values[values == cdf.attget("FILLVAL", variable)["Data"]] = np.nan
  46. if "VALIDMIN" in cdf.varattsget(variable):
  47. values[values < cdf.attget("VALIDMIN", variable)["Data"]] = np.nan
  48. if "VALIDMAX" in cdf.varattsget(variable):
  49. values[values > cdf.attget("VALIDMAX", variable)["Data"]] = np.nan
  50. return pd.DataFrame({"epoch": index, "X": values[:, 0], "Y": values[:, 1], "Z": values[:, 2]})
  51. year += 1
  52. output = pd.DataFrame(np.array(get_day_data('pg0', year, months[month], days[day])))
  53. result.append(output)
  54. print(result)

[包含结果的数据帧]
[包含结果的数据帧]
[包含结果的数据帧]

  1. 请注意,代码中有一些缩进错误,如果要运行它,需要将其修复。此外,代码中的结果将追加到`result`列表中,因此`result`中将包含多个数据帧。
  2. <details>
  3. <summary>英文:</summary>
  4. I want the following code to save all the outputs in an excel file but every time I try the code all I get the result of the last iteration saved. Yet, when I run it in idle, I get the second result.
  5. import urllib.request
  6. import datetime as dt
  7. from datetime import datetime, timezone, tzinfo
  8. import cdflib
  9. import numpy as np
  10. import pandas as pd
  11. from cdflib.epochs import CDFepoch
  12. import pathlib
  13. from xlsxwriter import Workbook
  14. from openpyxl import Workbook
  15. result = []
  16. months = [&#39;01&#39;,&#39;02&#39;,&#39;03&#39;,&#39;04&#39;,&#39;05&#39;,&#39;06&#39;,&#39;07&#39;,&#39;08&#39;,&#39;09&#39;,&#39;10&#39;,&#39;11&#39;,&#39;12&#39;]
  17. days = [&#39;01&#39;,&#39;02&#39;,&#39;03&#39;,&#39;04&#39;,&#39;05&#39;,&#39;06&#39;,&#39;07&#39;,&#39;08&#39;,&#39;09&#39;,&#39;10&#39;,&#39;11&#39;,&#39;12&#39;]
  18. for year in range(2014,2019,1):
  19. for month in range(len(months)):
  20. for day in range(len(days)):
  21. def get_day_data(station: str, year, month, day):
  22. is_pg = station.startswith(&quot;pg&quot;)
  23. if is_pg:
  24. url = f&quot;http://mist.nianet.org/CDFdata/{station}/{year}/thg_l2_mag_{station}_{year}{month}{day}_v01.cdf&quot;
  25. else:
  26. url = f&quot;https://ftp.space.dtu.dk/data/Ground_magnetometers/Adjusted/{station.upper()}/{year}/{month}/{station.upper()}_{year}{month}{day}.cdf&quot;
  27. file_name = url.split(&quot;/&quot;)[-1]
  28. if not pathlib.Path(file_name).exists():
  29. try:
  30. urllib.request.urlretrieve(url, &quot;tmp.cdf&quot;)
  31. pathlib.Path(&quot;tmp.cdf&quot;).rename(file_name)
  32. except:
  33. raise Exception(f&quot;Failed to download {url}&quot;)
  34. cdf = cdflib.CDF(file_name)
  35. if is_pg:
  36. epoch = cdf.varget(f&quot;thg_mag_{station}_time&quot;)
  37. index = pd.to_datetime(epoch, utc=False, unit=&quot;s&quot;)
  38. #index = index.round(&quot;1s&quot;)
  39. variable = f&quot;thg_mag_{station}&quot;
  40. else:
  41. epoch = cdf.varget(&quot;time&quot;)
  42. epoch = CDFepoch().to_datetime(epoch, to_np=True)
  43. index = pd.to_datetime(epoch)
  44. variable = &quot;HEZ&quot;
  45. values = cdf.varget(variable)
  46. if &quot;FILLVAL&quot; in cdf.varattsget(variable):
  47. values[values == cdf.attget(&quot;FILLVAL&quot;, variable)[&quot;Data&quot;]] = np.nan
  48. if &quot;VALIDMIN&quot; in cdf.varattsget(variable):
  49. values[values &lt; cdf.attget(&quot;VALIDMIN&quot;, variable)[&quot;Data&quot;]] = np.nan
  50. if &quot;VALIDMAX&quot; in cdf.varattsget(variable):
  51. values[values &gt; cdf.attget(&quot;VALIDMAX&quot;, variable)[&quot;Data&quot;]] = np.nan
  52. return pd.DataFrame({&quot;epoch&quot;: index,
  53. &quot;X&quot;: values[:, 0],
  54. &quot;Y&quot;: values[:, 1],
  55. &quot;Z&quot;: values[:, 2]})
  56. year += 1
  57. output = pd.DataFrame(np.array(get_day_data(&#39;pg0&#39;, year, months[month], days[day])))
  58. result.append(output)
  59. print(result)
  60. [ 0 1 2 3
  61. 0 2015-01-01 00:00:00.000999936 15152.530273 -79.599998 -53694.988281
  62. 1 2015-01-01 00:00:01.001000192 15152.589844 -79.599998 -53695.050781
  63. 2 2015-01-01 00:00:02.000999680 15152.589844 -79.599998 -53695.050781
  64. 3 2015-01-01 00:00:03.000999936 15152.650391 -79.599998 -53695.050781
  65. 4 2015-01-01 00:00:04.001000192 15152.650391 -79.599998 -53695.109375
  66. ... ... ... ... ...
  67. 86395 2015-01-01 23:59:55.000999936 15206.650391 -16.33 -53680.210938
  68. 86396 2015-01-01 23:59:56.000999680 15206.709961 -16.389999 -53680.210938
  69. 86397 2015-01-01 23:59:57.000999936 15206.769531 -16.389999 -53680.210938
  70. 86398 2015-01-01 23:59:58.001000192 15206.830078 -16.389999 -53680.210938
  71. 86399 2015-01-01 23:59:59.000999680 15206.830078 -16.389999 -53680.210938
  72. [86400 rows x 4 columns]]
  73. [ 0 1 2 3
  74. 0 2015-01-01 00:00:00.000999936 15152.530273 -79.599998 -53694.988281
  75. 1 2015-01-01 00:00:01.001000192 15152.589844 -79.599998 -53695.050781
  76. 2 2015-01-01 00:00:02.000999680 15152.589844 -79.599998 -53695.050781
  77. 3 2015-01-01 00:00:03.000999936 15152.650391 -79.599998 -53695.050781
  78. 4 2015-01-01 00:00:04.001000192 15152.650391 -79.599998 -53695.109375
  79. ... ... ... ... ...
  80. 86395 2015-01-01 23:59:55.000999936 15206.650391 -16.33 -53680.210938
  81. 86396 2015-01-01 23:59:56.000999680 15206.709961 -16.389999 -53680.210938
  82. 86397 2015-01-01 23:59:57.000999936 15206.769531 -16.389999 -53680.210938
  83. 86398 2015-01-01 23:59:58.001000192 15206.830078 -16.389999 -53680.210938
  84. 86399 2015-01-01 23:59:59.000999680 15206.830078 -16.389999 -53680.210938
  85. [86400 rows x 4 columns], 0 1 2 3
  86. 0 2016-01-02 00:00:00.000999936 15220.959961 -82.709999 -53625.089844
  87. 1 2016-01-02 00:00:01.001000192 15220.959961 -82.709999 -53625.089844
  88. 2 2016-01-02 00:00:02.000999680 15220.959961 -82.769997 -53625.199219
  89. 3 2016-01-02 00:00:03.000999936 15220.959961 -82.769997 -53625.320312
  90. 4 2016-01-02 00:00:04.001000192 15220.959961 -82.769997 -53625.441406
  91. ... ... ... ... ...
  92. 86395 2016-01-02 23:59:55.000999936 15125.44043 -124.110001 -53659.800781
  93. 86396 2016-01-02 23:59:56.000999680 15125.5 -124.110001 -53659.738281
  94. 86397 2016-01-02 23:59:57.000999936 15125.55957 -124.110001 -53659.738281
  95. 86398 2016-01-02 23:59:58.001000192 15125.55957 -124.110001 -53659.691406
  96. 86399 2016-01-02 23:59:59.000999680 15125.610352 -124.110001 -53659.691406
  97. [86400 rows x 4 columns]]
  98. [ 0 1 2 3
  99. 0 2015-01-01 00:00:00.000999936 15152.530273 -79.599998 -53694.988281
  100. 1 2015-01-01 00:00:01.001000192 15152.589844 -79.599998 -53695.050781
  101. 2 2015-01-01 00:00:02.000999680 15152.589844 -79.599998 -53695.050781
  102. 3 2015-01-01 00:00:03.000999936 15152.650391 -79.599998 -53695.050781
  103. 4 2015-01-01 00:00:04.001000192 15152.650391 -79.599998 -53695.109375
  104. ... ... ... ... ...
  105. 86395 2015-01-01 23:59:55.000999936 15206.650391 -16.33 -53680.210938
  106. 86396 2015-01-01 23:59:56.000999680 15206.709961 -16.389999 -53680.210938
  107. 86397 2015-01-01 23:59:57.000999936 15206.769531 -16.389999 -53680.210938
  108. 86398 2015-01-01 23:59:58.001000192 15206.830078 -16.389999 -53680.210938
  109. 86399 2015-01-01 23:59:59.000999680 15206.830078 -16.389999 -53680.210938
  110. [86400 rows x 4 columns],
  111. </details>
  112. # 答案1
  113. **得分**: 0
  114. 以下是您要翻译的代码部分:
  115. ```python
  116. import urllib.request
  117. import datetime as dt
  118. from datetime import datetime, timezone, tzinfo
  119. import cdflib
  120. import numpy as np
  121. import pandas as pd
  122. from cdflib.epochs import CDFepoch
  123. import pathlib
  124. from xlsxwriter import Workbook
  125. from openpyxl import Workbook
  126. def get_day_data(station, year, month, day):
  127. is_pg = station.startswith("pg")
  128. if is_pg:
  129. url = f"http://mist.nianet.org/CDFdata/{station}/{year}/thg_l2_mag_{station}_{year}{month}{day}_v01.cdf"
  130. else:
  131. url = f"https://ftp.space.dtu.dk/data/Ground_magnetometers/Adjusted/{station.upper()}/{year}/{month}/{station.upper()}_{year}{month}{day}.cdf"
  132. file_name = url.split("/")[-1]
  133. if not pathlib.Path(file_name).exists():
  134. try:
  135. urllib.request.urlretrieve(url, "tmp.cdf")
  136. pathlib.Path("tmp.cdf").rename(file_name)
  137. except:
  138. #raise Exception(f"Failed to download {url}")
  139. return None
  140. cdf = cdflib.CDF(file_name)
  141. if is_pg:
  142. epoch = cdf.varget(f"thg_mag_{station}_time")
  143. index = pd.to_datetime(epoch, utc=False, unit="s")
  144. #index = index.round("1s")
  145. variable = f"thg_mag_{station}"
  146. else:
  147. epoch = cdf.varget("time")
  148. epoch = CDFepoch().to_datetime(epoch, to_np=True)
  149. index = pd.to_datetime(epoch)
  150. variable = "HEZ"
  151. values = cdf.varget(variable)
  152. if "FILLVAL" in cdf.varattsget(variable):
  153. values[values == cdf.attget("FILLVAL", variable)["Data"]] = np.nan
  154. if "VALIDMIN" in cdf.varattsget(variable):
  155. values[values < cdf.attget("VALIDMIN", variable)["Data"]] = np.nan
  156. if "VALIDMAX" in cdf.varattsget(variable):
  157. values[values > cdf.attget("VALIDMAX", variable)["Data"]] = np.nan
  158. return pd.DataFrame({"epoch": index,
  159. "X": values[:, 0],
  160. "Y": values[:, 1],
  161. "Z": values[:, 2]})
  162. result = pd.DataFrame()
  163. months = ['01','02','03','04','05','06','07','08','09','10','11','12']
  164. days = ['01','02','03','04','05','06','07','08','09','10','11','12']
  165. for year in range(2015,2019,1):
  166. for month in range(len(months)):
  167. for day in range(len(days)):
  168. #output = pd.DataFrame(np.array(get_day_data('pg0', year, months[month], days[day])))
  169. output = get_day_data('pg0', str(year), months[month], days[day])
  170. if output is not None:
  171. result = pd.concat([result, output])
  172. print(result)
英文:

I tweaked it a little bit to test your code. It works after a few changes. Check if this is the output you expect.

  1. import urllib.request
  2. import datetime as dt
  3. from datetime import datetime, timezone, tzinfo
  4. import cdflib
  5. import numpy as np
  6. import pandas as pd
  7. from cdflib.epochs import CDFepoch
  8. import pathlib
  9. from xlsxwriter import Workbook
  10. from openpyxl import Workbook
  11. def get_day_data(station, year, month, day):
  12. is_pg = station.startswith(&quot;pg&quot;)
  13. if is_pg:
  14. url = f&quot;http://mist.nianet.org/CDFdata/{station}/{year}/thg_l2_mag_{station}_{year}{month}{day}_v01.cdf&quot;
  15. else:
  16. url = f&quot;https://ftp.space.dtu.dk/data/Ground_magnetometers/Adjusted/{station.upper()}/{year}/{month}/{station.upper()}_{year}{month}{day}.cdf&quot;
  17. file_name = url.split(&quot;/&quot;)[-1]
  18. if not pathlib.Path(file_name).exists():
  19. try:
  20. urllib.request.urlretrieve(url, &quot;tmp.cdf&quot;)
  21. pathlib.Path(&quot;tmp.cdf&quot;).rename(file_name)
  22. except:
  23. #raise Exception(f&quot;Failed to download {url}&quot;)
  24. return None
  25. cdf = cdflib.CDF(file_name)
  26. if is_pg:
  27. epoch = cdf.varget(f&quot;thg_mag_{station}_time&quot;)
  28. index = pd.to_datetime(epoch, utc=False, unit=&quot;s&quot;)
  29. #index = index.round(&quot;1s&quot;)
  30. variable = f&quot;thg_mag_{station}&quot;
  31. else:
  32. epoch = cdf.varget(&quot;time&quot;)
  33. epoch = CDFepoch().to_datetime(epoch, to_np=True)
  34. index = pd.to_datetime(epoch)
  35. variable = &quot;HEZ&quot;
  36. values = cdf.varget(variable)
  37. if &quot;FILLVAL&quot; in cdf.varattsget(variable):
  38. values[values == cdf.attget(&quot;FILLVAL&quot;, variable)[&quot;Data&quot;]] = np.nan
  39. if &quot;VALIDMIN&quot; in cdf.varattsget(variable):
  40. values[values &lt; cdf.attget(&quot;VALIDMIN&quot;, variable)[&quot;Data&quot;]] = np.nan
  41. if &quot;VALIDMAX&quot; in cdf.varattsget(variable):
  42. values[values &gt; cdf.attget(&quot;VALIDMAX&quot;, variable)[&quot;Data&quot;]] = np.nan
  43. return pd.DataFrame({&quot;epoch&quot;: index,
  44. &quot;X&quot;: values[:, 0],
  45. &quot;Y&quot;: values[:, 1],
  46. &quot;Z&quot;: values[:, 2]})
  47. result = pd.DataFrame()
  48. months = [&#39;01&#39;,&#39;02&#39;,&#39;03&#39;,&#39;04&#39;,&#39;05&#39;,&#39;06&#39;,&#39;07&#39;,&#39;08&#39;,&#39;09&#39;,&#39;10&#39;,&#39;11&#39;,&#39;12&#39;]
  49. days = [&#39;01&#39;,&#39;02&#39;,&#39;03&#39;,&#39;04&#39;,&#39;05&#39;,&#39;06&#39;,&#39;07&#39;,&#39;08&#39;,&#39;09&#39;,&#39;10&#39;,&#39;11&#39;,&#39;12&#39;]
  50. for year in range(2015,2019,1):
  51. for month in range(len(months)):
  52. for day in range(len(days)):
  53. #output = pd.DataFrame(np.array(get_day_data(&#39;pg0&#39;, year, months[month], days[day])))
  54. output = get_day_data(&#39;pg0&#39;, str(year), months[month], days[day])
  55. if output is not None:
  56. result = pd.concat([result, output])
  57. print(result)
  58. #Output
  59. # epoch X Y Z
  60. #0 2015-01-01 00:00:00.000999936 15152.530273 -79.599998 -53694.988281
  61. #1 2015-01-01 00:00:01.001000192 15152.589844 -79.599998 -53695.050781
  62. #2 2015-01-01 00:00:02.000999680 15152.589844 -79.599998 -53695.050781
  63. #3 2015-01-01 00:00:03.000999936 15152.650391 -79.599998 -53695.050781
  64. #4 2015-01-01 00:00:04.001000192 15152.650391 -79.599998 -53695.109375
  65. #... ... ... ... ...
  66. #86395 2018-12-12 23:59:55.000999936 14926.240234 -115.320000 -53617.519531
  67. #86396 2018-12-12 23:59:56.000999680 14926.299805 -115.320000 -53617.519531
  68. #86397 2018-12-12 23:59:57.000999936 14926.299805 -115.320000 -53617.519531
  69. #86398 2018-12-12 23:59:58.001000192 14926.299805 -115.320000 -53617.519531
  70. #86399 2018-12-12 23:59:59.000999680 14926.299805 -115.320000 -53617.519531
  71. #[32745600 rows x 4 columns]

huangapple
  • 本文由 发表于 2023年2月19日 11:03:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定