【问题标题】:How to have multiple arrays in python variable如何在python变量中有多个数组
【发布时间】:2018-07-23 19:46:02
【问题描述】:

现在我有一个小脚本,可以将数据写入和读取到 CSV 文件。

写函数的简要sn-p:

with open(filename,'w') as f1:
    writer=csv.writer(f1, delimiter=';',lineterminator='\n',)
    for a,b in my_function:
         do_things_to_get_data
         writer.writerow([tech_link, str(total), str(avg), str(unique_count)])

然后简单的sn-p读取文件:

infile = open(filename,"r")

for line in infile:
    row = line.split(";")
    tech = row[0]
    total = row[1]
    average = row[2]
    days_worked = row[3]
    do_things_with_each_row_of_data

我想一起跳过 CSV 部分,看看我是否可以将所有数据保存在一个变量中,但我不确定它是什么样的。任何帮助表示赞赏。

谢谢。

【问题讨论】:

  • “一起跳过 CSV 部分”是什么意思?您的问题不清楚,因此无法回答。
  • 我认为他是说他将程序的状态存储在 csv 文件而不是多维数组中。至少这似乎是我正在阅读的内容。在这种情况下,pythonprogramming.net/python-3-multi-dimensional-list
  • malan@ 是正确的。我将数据保存到 csv 文件只是为了稍后在脚本中读取它是没有意义的。

标签: python csv


【解决方案1】:

...no point in me saving data to a csv file just to read it later in the script

只需将其保存在列表列表中

data = []
for a,b in my_function:
     do_things_to_get_data
     data.append([tech_link, str(total), str(avg), str(unique_count)])

...

for tech, total, average, days_worked in data:
    do_things_with_each_row_of_data

可能值得将每个 保存为 namedtuple 或字典

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2021-07-22
    • 2014-02-23
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多