0

In one program I wrote, I saved the average price of BTC each day into a .txt file with this format:

Date,Price
"Jun 05, 2018",7567.330
"Jun 04, 2018",7618.500
"Jun 03, 2018",7676.170
"Jun 02, 2018",7590.080
"Jun 01, 2018",7521.070
"May 31, 2018",7450.160
"May 30, 2018",7438.120
...

In my code, I have a list of dates where I have to match the date in the list to a date in the textfile and find the average BTC price for that day. I am planning to save all of the dates and prices into a list each, called "coin_dates" and "coin_prices" respectively.

I tried to model my code after how a json dictionary is typically opened the difference being that I took away the "json.load(f)":

def initial_price(df):
    with open(df, "r") as f:
        coin_dates = [d["Date"] for d in f]
        coin_prices = [d["Price"] for d in f]

initial_price("btc.txt")

but I got this error:

Traceback (most recent call last):
   File "getICOdate.py", line 158, in <module>
     initial_price("btc.txt")
   File "getICOdate.py", line 155, in initial_price
     coin_dates = [d["Date"] for d in f]
   File "getICOdate.py", line 155, in <listcomp>
     coin_dates = [d["Date"] for d in f]
TypeError: string indices must be integers

I want to open the a txt files and save the dates and prices into two separate lists and I'm not sure how to approach it

2 Answers2

0

Firstly, you should consider taking your first program (or the first part of your program) that makes the plain text file (which sort of looks like csv) and rewrite it to make files that are either JSON or pickled python objects.

1- Dumping JSON to a file: If you convert your dictionary to JSON, you can simply do something like this with the JSON python library.

import json
with open('data.txt', 'w') as outfile:
    json.dump(data, outfile)

JSON will work well especially if other applications are reading this (same language or different language).

2- Python Pickling: If its python and only python, pickling works well too.

with open(fileName + '.pkl', 'wb') as f:
        pickle.dump(object_to_store, f, pickle.HIGHEST_PROTOCOL)

In summary, dont make it harder on yourself with the plain text file that you've made already. Generate something that is easy to load in and dump back out. JSON is practically the industry standard at this point and is super convenient to use on any platform! Let me know if you have specific questions.

Chad Van De Hey
  • 2,716
  • 3
  • 29
  • 46
  • So I tried saving my data into a .json file the way you told me and the new file was in the exact format as my .txt one except that there was an index part to it. Do you know how I can save it in a .json so it looks like this: [{"date" : "Jun 05, 2018", "price" : 7567.33}, {"date": "Jun 04, 2018", "price": 7618.50}] –  Jun 14 '18 at 04:21
  • You have to give me more info than that. What does your code look like (update this in the question)? Did you use json.dump()? Are you using json.loads() to reload the data? – Chad Van De Hey Jun 14 '18 at 17:39
  • See this SO link for examples:https://stackoverflow.com/questions/26745519/converting-dictionary-to-json-in-python?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Chad Van De Hey Jun 14 '18 at 17:40
  • @j.doe- you should request more info or mark an answer as correct – Chad Van De Hey Jun 15 '18 at 18:16
0

Did you try to print d to see what it contains? You would notice that it's just a string representing a line from your file. You are assuming that python automatically parsed this to an array for you, and even matched the keys to the header from the CSV file.

You have to parse your string first, or better yet, use a library that already does this for your. Here is an example with pandas:

import pandas as pd

df = pd.read_csv('btc.txt')
df[df["Date"] == "Jun 05, 2018"]["Price"] # 7567.330
Zac R.
  • 538
  • 3
  • 17
  • Although when I print out line 4 where I replace June 5 with a date in my list, I get this: "Name: Price, dtype: float64 301 3381.43" Do you know what I can do to only get the last number? –  Jun 14 '18 at 02:52
  • It prints out a bunch of other information that I don't need –  Jun 14 '18 at 04:25
  • try `df[df["Date"] == "Jun 05, 2018"]["Price"].iloc[0]` – Zac R. Jun 14 '18 at 11:25