简体   繁体   English

ValueError: 时间数据“无”与格式“%Y-%m-%dT%H:%M:%S.%f”不匹配

[英]ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f'

For the node 'TransactionDate' i have a logic before updating it for policy"POL000002NGJ".对于节点“TransactionDate”,我在更新策略“POL000002NGJ”之前有一个逻辑。 The logic i am trying to implement is If existing 'TransactionDate' < than today, then add 5 days with current value and parse it to xml.我试图实现的逻辑是如果现有 'TransactionDate' < 比今天,则用当前值添加 5 天并将其解析为 xml。

Transaction Date Format in XML : 2020-03-23T10:56:15.00 XML 格式的交易日期格式:2020-03-23T10:56:15.00

Please Note that, If i parsing the DateTime value like below, It works good But i dont want to hardcode the value... I want to Parse it as a string object to handle for any datetime in format ""%Y-%m-%dT%H:%M:%S.%f""...请注意,如果我像下面那样解析 DateTime 值,它运行良好但我不想对该值进行硬编码...我想将其解析为字符串对象以处理格式为 ""%Y-%m 的任何日期时间-%dT%H:%M:%S.%f""...

# <TransactionDate> 
today = datetime.now()
TransactionDate = doc.find('TransactionDate')
Date = '2020-03-24T10:56:15.00' 
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")

if previous_update < today:
     today = previous_update - timedelta(days=-5)
     TransactionDate = today.strftime("%Y-%m-%dT%H:%M:%S.%f")

Below code while parsing it as a DateTime Object, I have an issue.. I got struck here and referenced other answers in stackoverflow and python forums, But still i got struct up here and unable to resolve the issue...下面的代码在将其解析为 DateTime 对象时,我遇到了一个问题。

if any help to fix will be a great helpful.如果任何帮助修复将是一个很大的帮助。 Thanks.谢谢。 Below code using lxml and getting help to support below code will helpful.下面使用 lxml 的代码并获得支持以下代码的帮助会有所帮助。 Because i already completed for other nodes.因为我已经完成了其他节点。 My understanding is Date variable is calling as None.. But struck here to fix.. Please help..我的理解是 Date 变量调用为 None .. 但在这里击中修复.. 请帮助..

# <TransactionDate> 
today = datetime.now()
TransactionDate = doc.find('TransactionDate')
Date = str(TransactionDate)
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")

if previous_update < today:
     today = previous_update - timedelta(days=-5)
     TransactionDate = today.strftime("%Y-%m-%dT%H:%M:%S.%f")

Full Code is Below完整代码如下

from lxml import etree
from datetime import datetime, timedelta
import random, string


doc = etree.parse(r'C:\Users\python.xml') 

# <PolicyId> - Random generated policy number
Policy_Random_Choice = 'POL' + ''.join(random.choices(string.digits, k=6)) + 'NGJ'

# <TransactionDate> 
today = datetime.now()
TransactionDate = doc.find('TransactionDate')
Date = str(TransactionDate)  
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")

if previous_update < today:
     today = previous_update - timedelta(days=-5)
     TransactionDate = today.strftime("%Y-%m-%dT%H:%M:%S.%f")
                                  
#Parsing the Variables
replacements = [Policy_Random_Choice  , TransactionDate ]

targets = doc.xpath('//ROW[PolicyId="POL000002NGJ"]')
for target in targets:
    target.xpath('./PolicyId')[0].text = replacements[0]
    target.xpath('.//TransactionDate')[0].text = replacements[1]
 
print(etree.tostring(doc).decode())

Sample XML示例 XML

<TABLE>
   <ROW>
      <PolicyId>POL000002NGJ</PolicyId>
      <BusinessCoverageCode>COV00002D3X1</BusinessCoverageCode>
      <TransactionDate>2020-03-23T10:56:15.00</TransactionDate>
   </ROW>
   <ROW>
      <PolicyId>POL111111NGJ</PolicyId>
      <BusinessCoverageCode>COV00002D3X4</BusinessCoverageCode>
      <TransactionDate>2020-03-23T10:56:15.00</TransactionDate>
   </ROW>
</TABLE>

Maybe the find method is wrong.也许 find 方法是错误的。 Try this one试试这个

# <TransactionDate> 
today = datetime.now()
TransactionDate = doc.xpath('//ROW/TransactionDate') # Change find to xpath
Date = str(TransactionDate[0].text) # Use the first one
previous_update = datetime.strptime(Date, "%Y-%m-%dT%H:%M:%S.%f")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: 时间数据与格式 &#39;%Y-%m-%d %H:%M:%S.%f&#39; 不匹配 - ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f' ValueError: 时间数据 &#39;&#39; 与格式 &#39;%Y-%m-%dT%H:%M:%S&#39; 不匹配 - ValueError: time data '' does not match format '%Y-%m-%dT%H:%M:%S' ValueError: 时间数据“无”与格式“%Y-%m-%d %H:%M:%S”不匹配 - ValueError: time data 'None' does not match format '%Y-%m-%d %H:%M:%S' ValueError:时间数据“”与格式“%Y-%m-%dT%H:%M:%SZ”不匹配 - ValueError: time data '' does not match format '%Y-%m-%dT%H:%M:%SZ' ValueError:&#39;z&#39;是错误的伪指令,格式为&#39;%Y-%m-%dT%H:%M:%S.%f%z&#39;在熊猫0.23.4中而不是0.24.2中吗? - ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S.%f%z' in pandas 0.23.4 and not 0.24.2? 值错误:时间数据&#39;2019-03-19T07:01:02Z&#39;与格式&#39;%Y-%m-%dT%H:%M:%S.%fZ&#39;不匹配 - Value Error :Time data '2019-03-19T07:01:02Z' does not match format '%Y-%m-%dT%H:%M:%S.%fZ' ValueError:时间数据“ 140120 1520”与格式“%Y-%m-%d%H:%M:%S”不匹配 - ValueError: time data '140120 1520' does not match format '%Y-%m-%d %H:%M:%S' 出现错误“ValueError:时间数据&#39;&#39;与格式&#39;%Y-%m-%d %H:%M:%S&#39;不匹配” - Getting error "ValueError: time data '' does not match format '%Y-%m-%d %H:%M:%S'" 时间数据“2019-06-02T16:19:27.000-04:00”与格式“%Y-%m-%dT%H:%M:%S.%fZ”不匹配 - time data '2019-06-02T16:19:27.000-04:00' does not match format '%Y-%m-%dT%H:%M:%S.%fZ' Python 与格式 '%Y-%m-%dT%H:%M:%S%Z.%f' 不匹配 - Python does not match format '%Y-%m-%dT%H:%M:%S%Z.%f'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM