Guest User

Untitled

a guest
Nov 4th, 2021
45
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import datetime
  2. from bs4 import BeautifulSoup
  3. import re
  4. import os
  5. import sys
  6. import pytz
  7.  
  8. firstCorrectTime = ""
  9. previousCorrectTime = ""
  10. previousWrongTime = ""
  11. newTime = ""
  12. referenceDate= datetime.datetime(2072,1,1,0,0,1,1)
  13. time_zone = pytz.timezone('Europe/Budapest')
  14. referenceDate = time_zone.localize(referenceDate)
  15.  
  16. with open(input("Kérem a feldolgozandó fájl nevét: "),'r') as f:
  17.     xml = f.read()
  18.     bx = BeautifulSoup(xml)
  19.     times = bx.find_all('time')
  20.     for stime in times:
  21.         timestr = stime.text
  22.         rawtime = datetime.datetime.strptime(timestr,'%Y-%m-%dT%H:%M:%S.%f%z')
  23.         if(rawtime < referenceDate):
  24.             firstCorrectTime = rawtime
  25.         else:
  26.             if(previousWrongTime == ""):
  27.                 previousWrongTime = rawtime
  28.                 newTime = firstCorrectTime + datetime.timedelta(seconds = 2)
  29.             else:
  30.                 differenceInSeconds = (rawtime-previousWrongTime).seconds
  31.                 newTime = previousCorrectTime + datetime.timedelta(seconds = differenceInSeconds)
  32.  
  33.             #kicseréljük a rossz időbélyeget az újonnan kszámítottra
  34.             stime.string.replace_with(newTime.strftime('%Y-%m-%dT%H:%M:%S.%f%z'))
  35.  
  36.             previousCorrectTime = newTime
  37.             previousWrongTime = rawtime
  38.  
  39.  
  40.     with open(input("Kérem a kiírandó fájl nevét: "),'w') as g:
  41.         g.write(bx.prettify())
  42.  
  43.     print ('converted %s to %s' % ('items.xml','new_items.xml'))
  44.     sys.stdout.flush()
RAW Paste Data