我有几个问题要问我的密码。我希望它张贴的日期和价格列表的最高和最低的价值。现在它没有发布列,我尝试将我的定义调用为final_list,并在print列请求时在print语句中实现。它在输出中返回“无”。它不起作用。所以现在我想知道我的定义是否返回正确的值。
中返回了正确的值吗?
如果没有要实现的变量,那么
我目前的输出是:
Enter a file name: table.csv
Which column: 5
Lowest 5:
Highest 5:
do you want to continue? 我希望我的输出是:
Which column: 6
Lowest 6 for column 6
Date:08-1985, Value: 1.70
Date:09-1985, Value: 1.77
Date:06-1985, Value: 1.84
Date:10-1985, Value: 1.90
Date:07-1985, Value: 1.93
Date:11-1985, Value: 2.19
Highest 6 for column 6
Date:09-2012, Value:674.54
Date:08-2012, Value:635.39
Date:10-2012, Value:628.17
Date:04-2012, Value:597.19
Date:07-2012, Value:592.33
Date:03-2012, Value:569.11# Project No.: 5
# Author:
# Description: making definitions, reading from a .csv file, validating input, data-mining,
f_list = list()
file_object = ()
my_tot = dict()
new_list = list()
def get_input_descriptor():
while True:
filename = input("Enter a file name: ")
if filename == 'table.csv':
with open(filename, "r") as infile:
infile.readlines()[1:]
# for line in lines:
# print(line.rstrip())
break
else:
print("Bad file name, try again")
return file_object
def get_data_list(file_object, column_number):
dict = {}
for val in file_object:
date = val.split(",")[0]
data = float(val.split(",")[column_number])
dict[date] = data
return dict.items()
def average_data(new_list):
for date, price in new_list:
my_tot[date] = my_tot.get(date, 0) + float(price)
my_times[date] = my_times.get(date, 0) + 1
for key in my_tot:
f_list.append((float(my_tot[key] / my_times[key]), key))
def main():
get_input_descriptor()
column_number = int(input("Which column: "))
date_list = get_data_list(file_object, column_number)
final_list = average_data(date_list)
x = sorted(f_list)
print('Lowest', str(column_number) + ":")
for tup in x[:6]:
print
tup[0], tup[1]
print('Highest', str(column_number) + ":")
x = sorted(f_list, reverse=True)
for tup in x[:6]:
print
tup[0], tup[1]
while 1:
flag = input("do you want to continue? ")
if flag == '' or not flag[0].lower() in ['y', 'n']:
print("Please answer with a yes or no")
else:
break
if flag[0].lower() == 'y':
column = input("Which column: ")
print(column)
if flag[0].lower() == 'n':
print("Bye!")
if __name__ == "__main__":
main()csv.file看起来如下(前几行,总共1700行):
Date,Open,High,Low,Close,Volume,Adj Close
2013-02-08,474,478.81,468.25,474.98,22597100,474.98
2013-02-07,463.25,470,454.12,468.22,25163600,468.22
2013-02-06,456.47,466.5,452.58,457.35,21203800,454.7
2013-02-05,444.05,459.74,442.22,457.84,20476700,455.19
2013-02-04,453.91,455.94,442,442.32,17039900,439.76
2013-02-01,459.11,459.48,448.35,453.62,19267300,450.99
2013-01-31,456.98,459.28,454.98,455.49,11404800,452.85
2013-01-30,457,462.6,454.5,456.83,14898400,454.18
2013-01-29,458.5,460.2,452.12,458.27,20398500,455.61
2013-01-28,437.83,453.21,435.86,449.83,28054200,447.22
2013-01-25,451.69,456.23,435,439.88,43143800,437.33
2013-01-24,460,465.73,450.25,450.5,52173300,447.89
2013-01-23,508.81,514.99,504.77,514.01,30768200,511.03
2013-01-22,504.56,507.88,496.63,504.77,16483800,501.85
2013-01-18,498.52,502.22,496.4,500,16890100,497.1
2013-01-17,510.31,510.75,502.03,502.68,16202800,499.77
2013-01-16,494.64,509.44,492.5,506.09,24671600,503.16
2013-01-15,498.3,498.99,483.38,485.92,31313300,483.1
2013-01-14,502.68,507.5,498.51,501.75,26221700,498.84谢谢你,希望这是详细而清晰的。
发布于 2021-04-08 14:54:18
你的CSV文件有六列?每个整数中的数据是什么,输入整数的人如何知道哪些数据是可用的?
显然,您只对日期和值感兴趣,而忽略了其他列中的数据。
因此,我可能要做的是,在逐行读取CSV文件时,尝试将其写入字典,如下所示:
dict = {
"08-1985": "1.70",
"06-1985": "1.77",
"09-1985": "1.84"
}那么,您可以轻松地返回六个键值对,它们要么具有最新的日期、最老的日期、最大的值或最小的值?
如果告诉用户这一对是“日期:值”很重要,您可以在从字典中调用键或值之前,只需打印这些信息。
保存整个"Date: 08-1985"和"Value: 1.70"字符串似乎会带来不必要的麻烦。当我第一次读取CSV数据时,我会使用分隔符":“和",”并且只保留要使用的实际信息。
发布于 2021-04-08 20:03:08
根据对前一个答案的注释,这里有一种不同的方法,假设"date: "和"value: "是列标题,而不是csv文件中行数据的一部分:
# script for sorting nested lists
# adapted from https://www.geeksforgeeks.org/python-sort-list-according-second-element-sublist/
import csv
mycsv="C:\\Users\\mobarget\\Google Drive\\mycsv.csv"
table=[row for row in csv.reader(open(mycsv)) if row]
print(table)
# output should look like this:
# [['08-1985', '1.70'], ['07-1966', '1.57'], ['01-1982', '1.89']]
# Sort first element in nested lists
def sortdate(nested):
l = len(nested)
for i in range(0, l):
for j in range(0, l-i-1):
if (nested[j][0] > nested[j + 1][0]):
tempo = nested[j]
nested[j]= nested[j + 1]
nested[j + 1]= tempo
return nested
# Sort second element in nested lists
def sortvalue(nested):
l = len(nested)
for i in range(0, l):
for j in range(0, l-i-1):
if (nested[j][1] > nested[j + 1][1]):
tempo = nested[j]
nested[j]= nested[j + 1]
nested[j + 1]= tempo
return nested
# Print first six items in sorted lists
print(sortdate(table)[:6])
print(sortvalue(table)[:6])我的输入文件有2列和10行,如下所示:
01-1678, 1.34
03-1678, 1.54
02-1567, 1.92
10-1267, 1.89
11-1567, 1.90
12-1679, 1.06
03-1465, 1.78
04-1577, 1.38
05-1898, 1.22
06-1966, 1.66按日期排序的输出如下:
[['01-1678', ' 1.34'], ['02-1567', ' 1.92'], ['03-1465', ' 1.78'], ['03-1678', ' 1.54'], ['04-1577', ' 1.38'], ['05-1898', ' 1.22']]按值排序的输出是:
[['12-1679', ' 1.06'], ['05-1898', ' 1.22'], ['01-1678', ' 1.34'], ['04-1577', ' 1.38'], ['03-1678', ' 1.54'], ['06-1966', ' 1.66']]按值排序很好,但日期当然是作为字符串读取的,因此排序不正确。因此,本专栏中的数据首先需要转换为YYYY格式,这样脚本才能工作。但也许这是个好办法..。
https://stackoverflow.com/questions/67006221
复制相似问题