1

I am working on a small program that reads data from a CSV file. As part of the program, user input is used to only select data that >= but I get TypeError: unorderable types: str() >= int() when I run the code. Looks like the sting is not converting to integer.

def get_csv_data(data_type, num):
    import csv
    ga_session_data = {}
    ga_pageviews_data = {}

    with open('files/data.csv', 'r') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            page, sessions, pageviews = row
            ga_session_data[page] = int(sessions)
            ga_pageviews_data[page] = int(pageviews)

        if data_type == 'sessions' and sessions >= int(num):
            for page, sessions in ga_session_data.items():
                print(page, ' - ', sessions)

        elif data_type == 'pageviews' and pageviews >= int(num):
            for page, pageviews in ga_pageviews_data.items():
                print(page, ' - ', pageviews)

def main():
    while(True):
        question = input("Are you interested in sessions or pageviews?")

        if question == 'sessions':
            number = int(input("What range are you interested in?"))
            get_csv_data(data_type = 'sessions', num = int(number))

        elif question == 'pageviews':
            number = input("What range are you interested in?")
            get_csv_data(data_type = 'pageviews', num = int(number))

        else:
            print("Invalid Input. Choose between sessions and pageviews.")
main()
2
  • 2
    In sessions >= int(num), you're comparing a string (sessions) with an int. Commented Jul 28, 2016 at 16:51
  • same is true for pageviews >= int(num) Commented Jul 28, 2016 at 16:52

1 Answer 1

3

int does not cast its parameters to integer in-place. In fact those parameters are immutable.

int(sessions) does not exactly do what you think it does. session is not modified, but the return value of that call is an int.

You should assign the returned value to a new/same name:

sessions = int(sessions)
pageviews = int(pageviews)

The operator >= can now compare the two variables you have, since they are now both integers.


You may also want to rewrite that if block like so:

if data_type == 'sessions':
     for page, sessions in ga_session_data.items():
         if sessions >= int(num):
             print(page, ' - ', sessions)

In this way, you're actually checking the sessions count in the dictionary and not the sessions from the for loop.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. I changed it and the error went away but for some reason nothing gets returned after I input the number
@user3088202 But you are not returning anything in your function?
Sorry just started learning Python. Are you talking about main function or get_csv_data
get_csv_data. What output were you expecting?
page name and number of sessions/pageviews. Basicly, pull all page names and sessions where sessions total is >= some number
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.