Python - 相同的代码,相同的pc;但只有1个得到TypeError:(pywintypes.datetime)

时间:2018-06-05 21:43:12

标签: python typeerror python-datetime

我最近编写了一个Python脚本,它在一台电脑上运行完美,但在另一台电脑上却没有。我已经确认这两台机器都具有相同版本的Python(3.6.0)和完全相同的Anaconda版本(4.3.1 64位)

代码:

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

现在虽然它在我的机器上运行顺利,当我尝试在另一台PC上运行时,我得到以下错误

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'

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

1 个答案:

答案 0 :(得分:-1)

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

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