我正在研究一個從CSV文件中讀取數據的小程序。作爲程序的一部分,用戶輸入僅用於選擇數據> =但我得到TypeError:無法訂購的類型:str()> = int()當我運行代碼。看起來st不是轉換爲整數。Python 3 int()函數不會將輸入字符串轉換爲整數
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()
在'sessions> = int(num)'中,你正在比較一個字符串('sessions')和一個int。 –
頁面瀏覽也是如此> = int(num) –