Created
January 13, 2014 09:51
-
-
Save Azazeo/8397359 to your computer and use it in GitHub Desktop.
Custom csv-file processor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Room(): | |
@classmethod | |
def _process_calendar(self, s): | |
return [[x.split(':')[0],x.split(':')[1]] for x in s.replace('"','').replace(' ','').replace('{','').replace('}','').split(',')] | |
def __init__(self, s): | |
a = s[0:-1].split('\t') | |
self.url = a[0] | |
self.date_parsed = a[1] | |
self.bedrooms = a[9] | |
self.zipcode = a[12] | |
self.rating = a[13] | |
self.calendar = Room._process_calendar(s.split('\t')[14]) | |
def __next__(self): | |
if self.calendar: | |
day = self.calendar.pop() | |
return "{}\t{}\t{}\t{}\t{}\t{}\t{}".format(self.url, self.date_parsed, self.bedrooms, self.rating, self.zipcode, day[0], day[1]) | |
else: | |
raise StopIteration | |
def __iter__(self): | |
return self | |
def __repr__(self): | |
return (self.url + ' ' + self.date_parsed + '\n' +\ | |
self.bedrooms + ' ' + self.rating + ' ' + self.zipcode + '\n') | |
def show_calendar(self): | |
for k, v in self.calendar: | |
print (k + '\t' + v) | |
def return_calendar(self): | |
return self.calendar | |
i = 0 | |
o = open('E:\\homeaway_flat.csv', 'w') | |
o.write('url\tdate_parsed\tbedrooms\trating\tzipcode\tprice_date\tprice\n') | |
with open('E:\\homeaway_rooms.csv', 'r') as f: | |
k = f.readline() | |
while k != '': | |
try: | |
i = i + 1 | |
if i % 100 == 0: | |
print (i) | |
k = f.readline() | |
r = Room(k) | |
if r.zipcode == '60657' or r.zipcode == '60614' or r.zipcode == '60610': | |
print(r) | |
for s in r: | |
o.write(s+'\n') | |
except: | |
pass | |
o.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment