Python 3 题目 读取分析信息

zipcodes.py

# -*- coding :UTF-8 -*-
from  datetime import datetime,timedelta,timezone
import csv

class Zipcode:
    def __init__(self,dic):

        self.utctime = datetime.utcnow().replace(tzinfo=timezone.utc)
        self.zipcode = dic['zipcode']
        self.timezone = dic['timezoneDiff']
        self.time_zone = int(self.timezone)
        self.location = Location(dic['city'],dic['state'])
        self.geolocation = Geolocation(dic['latitude'],dic['longitude'])
        self.daylightSaving = dic['observeDaylightSavings']
        self.corresponding_time = self.utctime.astimezone(timezone(timedelta(hours=self.time_zone)))
    # override
    def __str__(self):
        return  'zipcode:{}  city:{}  state:{}  latitude:{}  longitude:{}  timezoneDiff:{}  observeDaylightSavings:{}  Corresponding_time:{}'.format(
            self.zipcode,self.location.city,self.location.state,self.geolocation.latitude,self.geolocation.longitude,
            self.timezone,self.daylightSaving,self.corresponding_time)

class Geolocation:
    def __init__(self,latitude,longitude):
        self.latitude = latitude
        self.longitude = longitude

class Location:
    def __init__(self,city,state):
        self.city = city
        self.state = state


allDic = {}
def readCSV(path):
    try:
        with open(path, 'r') as f:
            read = csv.reader(f)
            csv_keys = next(read)
            csv_reader = csv.DictReader(f, fieldnames=csv_keys)
            for row in csv_reader:
                d = {}
                for k, v in row.items():
                    d[k] = v
                allDic[d['zipcode']]=Zipcode(d)
                #print(allDic[d['zipcode']])
            return allDic
    except FileNotFoundError:
        print("Didn't find")
    except:
         raise
    finally:
        if f:
            f.close()

#Plz read the csv file first!
#path = r'S:\temp\test.csv'
#all_csv_info =readCSV(path)

findBy.py

# -*- coding :UTF-8 -*-

import zipcodes

#Plz read csv file first
path =r'S:\temp\test.csv'
all_csv_info =zipcodes.readCSV(path)

def findByZipcode(input_zipcode):
    str_key = str(input_zipcode)
    result = zipcodes.allDic.get(str_key,0)
    return result

def findInSameState(zipcode,records):
    corr_info = []
    sublist =[]
    for x in zipcode:
        for y in records:
            temp_state = y.location.state
            for t in records:
                temp_state2 = t.location.state
                if temp_state == temp_state2:
                    sublist.append(t)
            corr_info.append(sublist)
    return corr_info
def findByState(state):
    for x in state:
        temp = x
        for key in zipcodes.allDic:
            value = zipcodes.allDic[key]
            if temp == value.location.state:
                print(value)


'''
#Testing for findByState:
input_state = list(map(str,input('State(Plz separated by spaces):').split()))
TheCorrespondingInfo= findByState(input_state)
'''

'''
#Testing for findByZipcode:
input_zipcode = input('ZipCode:')
TheCorrespondingInfo=findByZipcode(input_zipcode)
print(TheCorrespondingInfo)
'''

 

你可能感兴趣的:(python从入门到放弃)