Home > Enterprise >  A function that opens a file and when it finds a certain string it returns a dictionary x next value
A function that opens a file and when it finds a certain string it returns a dictionary x next value

Time:11-08

Im trying to build a program that does stock-analys.

I have a file that contains a string (the company name) followed by a lot of values. I want my function to open the txt file and read trough it until it find the given variable name e.x "Apple", and then it should return a dictonary of the 30 next values after.

E.g i need the values 7.15, 6,72 .. after "apple" and the dates in the following example.

02-09-26    4.12
02-09-27    3.92
02-09-30    3.37
Apple
02-08-01    7.15
02-08-02    6.72
02-08-05    5.56
02-08-06    5.49
02-08-07    5.63

should be returning {(date1,vaule1)...}

def openfile(file_name, company_name):

    file_words = []
    with open(file_name, "r") as file:
        for line in file:

i really have no idea how to continue here..

CodePudding user response:

create a variable that indicates if you've reached the company name - found (value False) then go through the lines of the file, if line == company_name change the value of found to True

once the company name is reached, (you will move to elif Found) now you will check if the row starts with a date (format year-month-day, you can change that), if it doesn't start with a date you break the loop and return the file_word list, if it starts with a date you check that the line is longer than 8 chars (to check if it has more things and not only the date) if it is longer you append (date, price) to file_words

def openfile(file_name, company_name):
    file_words = []
    with open(file_name, "r") as file:
        lines = file.read().split("\n")  # read the file and split by lines
    found = False
    for line in lines:
        # wait until getting to the row of the company name
        if not found and line.strip() == company_name:
            found = True
        # company name row found, extract all dates and values until next company name
        elif found:
            if line.strip() == "":  # check that it isn't just an empty line
                continue
            try:
                datetime.datetime.strptime(line[:8], "%y-%m-%d")
            except ValueError:  # row doesn't start with a date, finished
                break
            # 8 is the length of the date so len(line) should be bigger than 8
            if len(line) > 8:
                # append (date, price) to file_words
                file_words.append((line[:8], line[8:].strip()))
    return file_words

# print(openfile("file_name", "Apple"))  # the file contains the data in your question
# [('02-08-01', '7.15'), ('02-08-02', '6.72'), ('02-08-05', '5.56'), ('02-08-06', '5.49'), ('02-08-07', '5.63')]

CodePudding user response:

I would do this this way:


from typing import Dict
from os.path import exists
from re import match

def load_stocks(source: str) -> Dict[str, Dict[str, float]]:
    """
    Function which loads data from a file and puts it into dictionary.
    @param source: Source file to get data from.
    @returns:      Dictionary of company names, each company has dictionary
                   of date-value pairs.
    """
    output = {}
    if exists(source):  # Check if file exists.
        company = ''  # Company name.
        with open(source, 'r') as file:
            for line in file:
                # If line has letters, it is likely a company name ...
                if match(r'[A-z]', line):
                    # Set current company name and create key in dict.
                    company = line.replace('\n', '')
                    output[company] = {}  # Place for stock data.
                # Otherwise if company is set ...
                elif company:
                    pair = line.split(' ')  # Divide line using space as delimeter.
                    # First and last element is date and value respectively.
                    date, value = pair[0], pair[-1].replace('\n', '')
                    # Create new entry with date as key and stock as value.
                    output[company][date] = float(value)
    return output

print(load_stocks('./stock.txt'))
  • Related