Python ValueError:未转换的数据仍然存在:02:05

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20327937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:12:53  来源:igfitidea点击:

ValueError: unconverted data remains: 02:05

pythondatedatetimepython-2.7

提问by 4m1nh4j1

I have some dates in a json files, and I am searching for those who corresponds to today's date :

我在 json 文件中有一些日期,我正在搜索与今天日期相对应的日期:

import  os
import time
from datetime import datetime
from pytz import timezone

input_file  = file(FILE, "r")
j = json.loads(input_file.read().decode("utf-8-sig"))

os.environ['TZ'] = 'CET'

for item in j:
    lt = time.strftime('%A %d %B')
    st = item['start']
    st = datetime.strptime(st, '%A %d %B')

    if st == lt :
        item['start'] = datetime.strptime(st,'%H:%M') 

I had an error like this :

我有这样的错误:

File "/home/--/--/--/app/route.py", line 35, in file.py

st = datetime.strptime(st, '%A %d %B')

File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime

data_string[found.end():])

ValueError: unconverted data remains: 02:05

Do you have any suggestions ?

你有什么建议吗 ?

采纳答案by Maxime Lorant

The value of stat st = datetime.strptime(st, '%A %d %B')line something like 01 01 2013 02:05and the strptimecan't parse this. Indeed, you get an hour in addition of the date... You need to add %H:%Mat your strptime.

statst = datetime.strptime(st, '%A %d %B')行的值类似于01 01 2013 02:05并且strptime无法解析此内容。实际上,除了日期之外,您还有一个小时......您需要%H:%M在您的 strptime添加。

回答by Martijn Pieters

You have to parse allof the input string, you cannot just ignore parts.

您必须解析所有输入字符串,不能只是忽略部分。

from datetime import date, datetime

for item in j:
    st = datetime.strptime(item['start'], '%A %d %B %H:%M')

    if st.date() == date.today():
        item['start'] = st.time()

Here, we compare the date to today's date by using more datetimeobjects instead of trying to use strings.

在这里,我们通过使用更多datetime对象而不是尝试使用字符串来将日期与今天的日期进行比较。

The alternative is to only pass in partof the item['start']string (splitting out just the time), but there really is no point here, not when you could just parse everything in one step first.

另一种方法是仅通过部分中的item['start']字符串(分裂出去只是时间),但实在是没有点在这里,而不是当你可以只解析一切都在一步第一。

回答by 4m1nh4j1

Well it was very simple. I was missing the format of the date in the json file, so I should write :

嗯,这很简单。我错过了 json 文件中日期的格式,所以我应该写:

st = datetime.strptime(st, '%A %d %B %H %M')

because in the json file the date was like :

因为在 json 文件中,日期是这样的:

"start": "Friday 06 December 02:05",

回答by anjaneyulubatta505

Best answer is to use the from dateutil import parser.

最佳答案是使用from dateutil import parser.

usage:

用法:

from dateutil import parser
datetime_obj = parser.parse('2018-02-06T13:12:18.1278015Z')
print datetime_obj
# output: datetime.datetime(2018, 2, 6, 13, 12, 18, 127801, tzinfo=tzutc())

回答by Vishal Kanaujia

  timeobj = datetime.datetime.strptime(my_time, '%Y-%m-%d %I:%M:%S')
  File "/usr/lib/python2.7/_strptime.py", line 335, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains:

In my case, the problem was an extra space in the input date string. So I used strip()and it started to work.

就我而言,问题是输入日期字符串中有一个额外的空格。所以我使用了strip()它并开始工作。