0

OK, This is probably pretty simple, but I'm still very new to python. I appreciate the help.

I'm trying to get my program to access a text file indicated from the command line that contains some float data preceded on a line but a related year.

For example:

2010
11 21.5 18 15.2 13 17 14 0 10 16 8.6 5
2013
10.6 14.5 17.9 11.2 1.5 1.9 16 2.2 10 5.2 4.6 5

I want to ask the user to input a year, and then if the year is found in the text file, print the related data for each month.

I can't seem to get this to work.

Currently, the program will check for the year, and if it's not found it will say so, and ask again, but if it is found, it will just ask again immediately.

This is a tidied version of the part of my code:

f = open(sys.argv[1])

months = ['January', 'February', ...]

while True:
    f.seek(0)
    os.system('cls')
    year = input("Enter year for which you want rainfall data: ")
    line = f.readline().strip()
    while line and line != year:
        line = f.readline().strip()

    if not line:
        print("No rainfall data found for year {}".format(year))
        input("Press Enter to continue ...")
        response = input("Do it again for another year? [[y]/n] ")
        if response == "n":
            break

    else:
        rain_line = f.readline()
        rain_line = rain_line.strip()
        rain_line = rain_line.split()
        rain_strings = list(rain_line)
        rain_numbers = []
        for rain in rain_strings:
            rain_numbers.append(rain)

    print("in {},  {} Millimeters.".format(months[1], rain_numbers[1]))

Anybody see if i'm close or way off?

I've been toying around for hours.

GnarlyTot
  • 21
  • 3

3 Answers3

0

Don't consider it one line and the next line, consider them as pairs. Using this pairwise iterator:

def parse_rainfall(f):
    for year, data in pairwise(f):
        # ...
Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

For your code, the problem is on the condition of the while expression while line and line != year:.

If you input like 2010 for year = input("Enter year for which you want rainfall data: "), it will be parsed to int type, and will never equal to the line, which is a string. Use a conversion str(year) will fix the problem.

But you may want to use a different method both for efficiency and coding.

If the file is not very large, try to parse the file first, making it into a Python dictionary like {2010: [11 21.5 18 15.2 13 17 14 0 10 16 8.6 5], ...}, then you can just query the dictionary by using the year as key and fetch its data.

If the file is large, consider to parse and write it to a database, either SQL or NoSQL, then the query becomes as simple as a using Python dictionary.

Neo X
  • 947
  • 7
  • 9
0

Otherwise, you can change how you used to search in your text file and use a solution like this (I didn't test it with a large file. Please let me know if it breaks with it).

I'm assuming your input file is called input_file:

def get_data():
    data = list(k.rstrip() for k in open("input_file", 'r'))
    d = list(data[k:k+2] for k in range(0,len(data),2))
    final = []
    while True:
        user_data = input("Enter a year: ")
        if user_data == 'q':
            break
        for k in d:
            if k[0] == user_data:
                final.append(k[1])

        if final != '':
            print("\n".join(final))
            final = []
        else:
            print("Cannot find any data for the year:", user_data)


get_data()

Demo:

Enter a year: 2010
11 21.5 18 15.2 13 17 14 0 10 16 8.6 5
Enter a year: 2013
10.6 14.5 17.9 11.2 1.5 1.9 16 2.2 10 5.2 4.6 5
Enter a year: 2000
Cannot find any data for the year: 2000
Enter a year: 2010
11 21.5 18 15.2 13 17 14 0 10 16 8.6 5
Enter a year: 2100
Cannot find any data for the year: 2100
Enter a year: 22
Cannot find any data for the year: 22
Enter a year: q
Chiheb Nexus
  • 9,104
  • 4
  • 30
  • 43