【问题标题】:Python - identical code, identical pc's; but only 1 gets TypeError: (pywintypes.datetime)Python - 相同的代码,相同的电脑;但只有 1 得到 TypeError: (pywintypes.datetime)
【发布时间】:2018-06-05 21:43:11
【问题描述】:

我有一个我最近编写的 Python 脚本,它可以在一台电脑上完美运行,但不能在另一台电脑上运行。我已验证两台机器的 Python (3.6.0) 版本和 Anaconda (4.3.1 64-Bit) 版本完全相同

代码:

import requests
from win32com.client import Dispatch
import win32com.client #as win32
import datetime
import os
import re
import pywintypes
import time


# First I obtain data from a given webpage using the 'requests' library
# .. and write it to file

text_file.write(re.sub(r'[^\x00-\x7f]',r'', result.text))

# Next, I parse the text file using  the below:  
    if 'startDate' in line:
        str = line
        str = str.replace('"','')
        str = str.replace(':','')
        str = str.replace('{','')
        str = str.replace('}','')
        str = str.replace('[','')
        str = str.replace(']','')
        str = str.replace('startDate','')
        str = str.replace("\n","")
        dt = datetime.datetime.fromtimestamp(int(str)/1000.0)
        str = dt.strftime("%m/%d/%y" + " %H:%M:%S %p")
        xlSht.Cells(i,6).Value = str
        #xlSht.Cells(i,6).Value = dt.strftime("%m/%d/%y")  + " %I:%M:%S %p")
        xlSht.Cells(i,6).NumberFormat = "[$-409]mm/dd/yy hh:mm:ss AM/PM;@"    

# Line that is parsed: "starDate":1528231802193},

# I then parse the field which will be compared to the above:

    if '"date":{"endDate":' in line: # Column U
        str = line
        str = str.replace('"','')
        str = str.replace(':','')
        str = str.replace('{','')
        str = str.replace('}','')
        str = str.replace('[','')
        str = str.replace(']','')
        str = str.replace('endDate','')
        str = str.replace("\n","")
        dt = datetime.datetime.fromtimestamp(int(str)/1000.0)
        xlSht.Cells(i,21).Value = dt.strftime("%m/%d/%y" + " %H:%M:%S %p") 
        xlSht.Cells(i,21).NumberFormat = "[$-409]m/d/yy hh:mm:ss AM/PM;@"

# Line that is parsed: "endDate":1528231801083},



# Finally I compare the two values within excel

xlApp = Dispatch("Excel.Application")
xlApp.Visible = 1
xlWb = xlApp.Workbooks.Add()
xlSht = xlWb.Worksheets(1)


x = 2
while len(xlSht.Cells(x,1).Text) > 0: 
    if xlSht.Cells(x,6).Value < xlSht.Cells(x,21).Value:
        xlSht.Cells(x,6).Value = xlSht.Cells(x,21).Value
    x += 1

# Example: xlSht.Cells(6, 1).Value = 06/07/18 10:45:56 AM
# Example: xlSht.Cells(x, 21).Value = 6/5/18 10:45:56 AM

现在它在我的机器上运行顺利,当我尝试在另一台电脑上运行它时,我收到以下错误

line 1428, in <module>...
if xlSht.Cells(x,6).Value < xlSht.Cells(x,21).Value:   

TypeError: '<' not supported between instances of 'pywintypes.datetime and 
'float'

我很困惑这怎么可能。非常感谢任何解决方案

【问题讨论】:

  • 认真的吗?错误信息中的操作符是','?
  • 为什么所有额外的进口?如果您将示例代码缩减为示例代码所需的内容,则更容易理解您的问题。此外,您很有可能自己对问题有更好的了解。
  • 我不认为这是您实际运行的代码或您实际收到的错误消息。请发布实际的错误消息和运行时实际产生错误消息的代码,并请复制粘贴,而不是重新输入代码或错误消息。
  • 整个代码有 1500 多行,我只是将其精简到我认为相关的内容。此外,我的代码中使用了每个库。
  • 您的代码中唯一的&gt; 用于比较一个int 和一个int。您包含的错误消息不是由此代码产生的。请发布完成错误消息。

标签: python typeerror python-datetime


【解决方案1】:

我认为您正在从 Excel 导入数据。在这种情况下,可能的问题是您正在比较两个不同类型的字段。 IE。正是错误告诉您的内容。尝试更改源数据。

换一种说法 - 两台 PC 可能运行相同版本的 Python,但它们是从同一个文件导入的吗?

【讨论】:

    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多