2017-10-06 16 views
0

Pythonのdateutil.parserにいくつかの問題が発生しました。ValueError:不明な文字列形式dateparser python CSV

私は月を読み込んで表示するように任命されたCSVファイルを持っています。

グループには29 Jun 2017 1600が含まれ、一部のデータにはhrsなどの追加の文字列がある場合は常に「29 Jun 2017 1600 hrs」が含まれます。それは、このエラーが表示されます。ValueError: Unknown string formatここ

は私のコードです:dt = dateutil.parser.parse(data.GetDataType(frequency))

どのように私はhrsを削除したり、プログラムをスムーズに実行できるようにすることができますか?

答えて

0
# full, working, python 3 solution 
# modified to work on strings with or without "hrs" 
# "$" in re.sub means hrs only matches at end of input string 
# I hope you read this, because I can change this answer, but I cannot comment :-D 

import re 
from datetime import datetime 

stringDateIn1 = "29 Jun 2017 1601" 
stringDateIn2 = "29 Jun 2017 1602 hrs" 

stringDateFixed1 = re.sub(" hrs$", "", stringDateIn1) 
stringDateFixed2 = re.sub(" hrs$", "", stringDateIn2) 

datetimeOut1 = datetime.strptime(stringDateFixed1, '%d %b %Y %H%M') 
datetimeOut2 = datetime.strptime(stringDateFixed2, '%d %b %Y %H%M') 

print("date = " + str(datetimeOut1)) 
print("date = " + str(datetimeOut2)) 
+0

一部のデータには時間がありますが、一部のデータには時間があります。 hrsの有無にかかわらずスムーズに動作するようにするにはどうすればよいですか?助けてくれてありがとう! –

関連する問題