質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
DateTime

多くのプログラミング言語におけるDateTimeオブジェクトは、日付と時間に関する演算と出力を行います。

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

1049閲覧

python エラーにについて

fruitpunch

総合スコア6

DateTime

多くのプログラミング言語におけるDateTimeオブジェクトは、日付と時間に関する演算と出力を行います。

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/06/03 11:01

from datetime import datetime def read_data(filename): records = [] with open ('data.txt','r') as readfile: for k in readfile: k = k.strip() list = k.split(',') date = datetime.strptime(list[0], '%d-%m-%Y') date1 = datetime.strftime(date, '%d/%m/%Y') branch = list[1] daily_sale = float(list[2]) transaction = int(list[3]) rec = [date1,branch,daily_sale,transaction] records.append(rec) return records def print_all_records(records): template = '{0:<20} {1:<15} {2:<15} {3:<5}' print('*' * 10 + ' Printing all records ' + '*' * 10) print(template.format('Date','Branch','Daily sale','Transaction')) print('-' * 64) template = '{0:<20} {1:<15} ${2:<15} {3:<5}' for k in records: print(template.format(k[0],k[1],k[2],k[3])) def query_branch_sale(records): print('*' * 10 + ' Querying total sales by branch ' + '*' * 10) branch_name = input('Please enter the branch name ').title() avarage_sale = 0 total_transaction = 0 total_sales = 0 avarage_transaction = 0 count = 0 for k in records: if branch_name == k[1]: total_sales += k[2] total_transaction += k[3] count += 1 if count == 0: avarage_sale = 0 avarage_transaction = 0 else: avarage_sale = total_sales/count avarage_transaction = total_transaction/count print('Total sale by ' + branch_name + ': ' + '$' + str(total_sales)) print('Average sale by ' + branch_name + ' $' + str(avarage_sale)) print('Total transaction by ' + branch_name + ' ' + str(total_transaction)) print('Average number of transaction per day by ' + branch_name + ' ' + str(avarage_transaction)) def query_record_by_date(records): print('*' * 10 + ' Querying a records by date ' + '*' * 10) date = input('Please enter a date (dd mm yyyy) ') user_date = datetime.strptime(date, '%d %m %Y') output_date = datetime.strftime(user_date, '%d/%m/%Y') count = 0 template = '{0:<20} {1:<15} ${2:<15} {3:<5}' for k in records: if output_date == k[0]: print(template.format(k[0],k[1],k[2],k[3])) ATV = round(k[2]/k[3],2) print('ATV for the day: ' + str(ATV)) break else: count += 1 if count == len(records): print('No record found for ' + date) def query_period_sale(records): print('*' * 10 + ' Querying sales between two date ' + '*' * 10) date1 = input('Please enter the start date(dd mm yyyy): ') date2 = input('Please enter the end date(dd mm yyyy): ') user_date0 = datetime.strptime(date1, '%d %m %Y') user_date1 = datetime.strptime(date2, '%d %m %Y') output_date0 = datetime.strftime(user_date0, '%d/%m/%Y') output_date1 = datetime.strftime(user_date1, '%d/%m/%Y') total = 0 for k in records: r_date = datetime.strptime(k[0], '%d/%m/%Y') if output_date0 <= r_date and output_date1 >= r_date: total += round(k[2],2) print('Total sale between ' + date1 + ' and ' + date2 + ' is $' + str(total)) def main(): records = read_data('data.txt') print_all_records(records) query_branch_sale(records) query_record_by_date(records) query_period_sale(records) main()

このようなエラーが出ました。Googleで検索しても解決法が見つからなかったので、どなたかコード見ていただけないでしょうか。
よろしくおねがいします。
if output_date0 <= r_date and output_date1 >= r_date:
TypeError: '<=' not supported between instances of 'str' and 'datetime.datetime'

ちなみにこのような感じに仕上げたいのですが、エラーがでるのは最後の図の部分です????????
イメージ説明
イメージ説明
イメージ説明
イメージ説明

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shiracamus

2020/06/03 11:24 編集

> TypeError: '<=' not supported between instances of 'str' and 'datetime.datetime' エラーメッセージのとおりなのでは? 文字列同士 あるいは datetime どおしの比較にしましょう。 >>> from datetime import datetime >>> output_date0 = datetime.strftime(datetime.now(), "%d/%m/%Y") >>> output_date0 '03/06/2020' >>> r_date = datetime.strptime("3/6/2020", '%d/%m/%Y') >>> r_date datetime.datetime(2020, 6, 3, 0, 0) >>> output_date0 <= r_date Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '<=' not supported between instances of 'str' and 'datetime.datetime'
guest

回答2

0

自己解決

from datetime import datetime def read_data(filename): #read data from a data.txt records = [] with open (filename,'r') as readfile: for k in readfile: k = k.strip() #separated the line by ',' date_list = k.split(',') # extract date info and convert from string to object date = datetime.strptime(date_list[0], '%d-%m-%Y') date1 = datetime.strftime(date, '%d/%m/%Y') branch = date_list[1] daily_sale = float(date_list[2]) transaction = int(date_list[3]) #create a record as a list rec = [date1,branch,daily_sale,transaction] records.append(rec) return records def print_all_records(records): #print out all records by template formart template = '{0:<20} {1:<15} {2:<15} {3:<5}' print('*' * 10 + ' Printing all records ' + '*' * 10) print(template.format('Date','Branch','Daily sale','Transaction')) print('-' * 64) #make the '$' in front of the daily sale number template = '{0:<20} {1:<15} ${2:<15} {3:<5}' for k in records: print(template.format(k[0],k[1],k[2],k[3])) print('\n\n') def query_branch_sale(records): #ask the user for a branch name print('*' * 10 + ' Querying total sales by branch ' + '*' * 10) branch_name = input('Please enter the branch name ').title() avarage_sale = 0 total_transaction = 0 total_sales = 0 avarage_transaction = 0 count = 0 for k in records: #find the branch name in records if branch_name == k[1]: total_sales += float(k[2]) total_transaction += k[3] count += 1 if count == 0: #if can't find display the result '0' avarage_sale = 0 avarage_transaction = 0 else: #calculate the average avarage_sale = float(total_sales/count) avarage_transaction = int(total_transaction/count) print('Total sale by ' + branch_name + ': ' + '$' + str(total_sales)) print('Average sale by ' + branch_name + ' $' + str(avarage_sale)) print('Total transaction by ' + branch_name + ' ' + str(total_transaction)) print('Average number of transactions per day by ' + branch_name + ' ' + str(avarage_transaction)) print('\n\n') def query_record_by_date(records): #asks from the user for a date print('*' * 10 + ' Querying a records by date ' + '*' * 10) date = input('Please enter a date (dd mm yyyy) ') user_date = datetime.strptime(date, '%d %m %Y') output_date = datetime.strftime(user_date, '%d/%m/%Y') count = 0 template = '{0:<20} {1:<15} ${2:<15} {3:<5}' for k in records: if output_date == k[0]: print(template.format(k[0],k[1],k[2],k[3])) #calculate ATV ATV = round(k[2]/k[3],2) print('ATV for the day: ' + str(ATV)) print('\n\n') break #finish if found the enter date else: count += 1 #If no record found for the entered date then displays ‘No record found’ if count == len(records): print('No record found for ' + date) print('\n\n') def query_period_sale(records): #asks the user for a start date and an end date. print('*' * 10 + ' Querying sales between two date ' + '*' * 10) date1 = input('Please enter the start date(dd mm yyyy): ') date2 = input('Please enter the end date(dd mm yyyy): ') user_date0 = datetime.strptime(date1, '%d %m %Y') user_date1 = datetime.strptime(date2, '%d %m %Y') total = 0 for k in records: r_date = datetime.strptime(k[0], '%d/%m/%Y') #if found both entered date then calculate the daily sale total between this two days if user_date0 <= r_date and user_date1 >= r_date: total += round(k[2],2) print('Total sale between ' + date1 + ' and ' + date2 + ' is $' + str(total)) print('\n\n') def main(): records = read_data('data.txt') print_all_records(records) query_branch_sale(records) query_record_by_date(records) query_period_sale(records) main()

解決できました。

  output_date0 = datetime.strftime(user_date0, '%d/%m/%Y')   output_date1 = datetime.strftime(user_date1, '%d/%m/%Y')

この二行が無駄でした。

投稿2020/06/05 07:13

fruitpunch

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

文字列とdatetimeは<=で比較できん!
とおっしゃってますが、こころあたりはあるでしょうか

投稿2020/06/03 11:23

y_waiwai

総合スコア87774

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fruitpunch

2020/06/03 13:31

回答ありがとうございます。 心当たりはありますが解決策がわからなかったので質問させていただきました。 どのようにコードを直せばよろしいのでしょうか? もしお時間がありましたらお教えいただけると嬉しいです。
y_waiwai

2020/06/03 13:34

「python 文字列 datetime」あたりでぐぐると、文字列をdatetimeに変換できるようになるでしょうから、そこからどーこーすればいいんではないかと。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問