I am doing a program where a user enters a text file (apple.dat) and a date for prices they want to see. For some reason I am only getting an output for 2020-01-02 and getting 'none' for all other inputs. If anyone has any ideas, would really appreciate it.
apple.dat file
Date,Open,High,Low,Close,Adj Close,Volume 2020-01-02,74.059998,75.150002,73.797501,75.087502,74.333511,135480400 2020-01-03,74.287498,75.144997,74.125000,74.357498,73.610840,146322800...etc input 2020-01-02
output
Enter the data file name: apple.dat Enter a date: 2020-01-02 Date:2020-01-02 Open:74.059998 High:75.150002 Low:73.797501 Close:75.087502 Adj Close:74.333511 Volume:135480400 None another input
2020-01-03 output
None import csv def dictt(file_name): perfect_dict=[] with open('apple.dat') as f: reader = list(csv.reader(f)) for row in reader[1:]: temp_dict = {} temp_dict["Date"] = row[0] temp_dict["Open"] = row[1] temp_dict["High"] = row[2] temp_dict["Low"] = row[3] temp_dict["Close"] = row[4] temp_dict["Adj Close"] =row[5] temp_dict["Volume"] = row[6] perfect_dict.append(temp_dict) return perfect_dict def price(file_name,input_date,dictionary): for each in dictionary: if each['Date'] == input_date: for each_key in each: print(f"{each_key}:{each[each_key]}") def main(): print("Welcome to the Stock Price Program") print() file_name=input("Enter the data file name: ") input_date=input("Enter a date: ") dictionary=dictt(file_name) pro=price(file_name,input_date,dictionary) print(pro) main() https://stackoverflow.com/questions/67260346/not-getting-output-for-certain-dictionary-keys-python April 26, 2021 at 11:06AM
没有评论:
发表评论