Python time-shaping to standard format sample sharing

Source: Internet
Author: User
Copy CodeThe code is as follows:


Import OS
Import Sys
Import Pickle
Import string
Import re
Import time
From datetime import Date
From csv_timestamp_datetime Import *


Ip_region_list = pickle.load (open (' Ip_region_list.pickle ', ' r '))
Ip_region_list.sort (Key=lambda x:x[0])
List_len = Len (ip_region_list)

def find (Key):
Begin = 0
End = List_len-1
while (begin <= End):
Middle = (begin + END)/2
If ip_region_list[middle][0] <= key and ip_region_list[middle][1] >= key:
return ip_region_list[middle][2]
Else
If ip_region_list[middle][0] >= key:
End = Middle-1
Else
Begin = middle + 1


Def ip2region ():
of = open (Sys.argv[1], ' R ')
NF = open (sys.argv[2], ' W ')
For line in of:
Items = Line.strip (). Split (', ')
Try
x = Time.localtime (Long (items[0))
Ymdhms = Time.strftime ('%y-%m-%d%h:%m:%s ', x)
Nf.write (', '. Join ([Ymdhms, Items[1], Long2ip (Long (items[2))]) + ' \ n ')
Except Exception as E:
Print E
Print Line
if (__name__ = = ' __main__ '):
Ip2region ()
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.