Python - идентичный код, идентичные ПК;но только 1 получает TypeError: (pywintypes.datetime) - PullRequest
0 голосов
/ 06 июня 2018

У меня есть скрипт 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

Теперь, когда он работает на моей машине без помех, когда я пытаюсь запустить его на другом компьютере, я получаю сообщение об ошибке ниже

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 голосов
/ 06 июня 2018

Я думаю, вы импортируете данные из Excel.В этом случае вероятная проблема заключается в том, что вы сравниваете два поля разных типов.Т.е. именно то, что говорит об ошибке.Попробуйте изменить исходные данные.

Другими словами - возможно, на двух компьютерах установлена ​​одна и та же версия Python, но импортируют ли они из одного файла?

...