def create_phone_files(dataframe):
# Drop all rows without a phone number
dataframe = dataframe.dropna(subset=['phone'])
dataframe['phone'] = dataframe['phone'].map(lambda x: re.sub('[^0-9]', '', str(x)))
# Break file up by phone
phone_numbers = dataframe.phone.unique()
phone_dataframe = {phone_number: pandas.DataFrame() for phone_number in phone_numbers}
for key in phone_dataframe.keys():
phone_dataframe[key] = dataframe[:][dataframe.phone == key]
# Check if file already exists for each location, if so then append, if not then create a new file
print 'Appending location data to existing files'
# Lock all processes while work is being done to save files
for key, value in phone_dataframe.iteritems():
if os.path.isfile('{0}phone_{1}.csv'.format(config['phone_data'], str(key))):
lock.acquire()
print 'lock has been set for file {0}'.format(file)
value.to_csv('{0}phone_{1}.csv'.format(config['phone_data'], str(key)), mode='a', header=False, encoding='utf-8')
lock.release()
else:
value.to_csv('{0}phone_{1}.csv'.format(config['phone_data'], str(key)), header=True, encoding='utf-8')
print 'finished file {0}'.format(file)
评论列表
文章目录