daily pastebin goal
54%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import MySQLdb
  3.  
  4. with open('data.csv', 'w') as file:
  5.     conn = MySQLdb.connect(
  6.  
  7.               )
  8.  
  9.     cursor = conn.cursor()
  10.     query = 'select id, fname, mname, lname, residence_add_l1, residence_add_l2, residence_city, residence_state, residence_pincode, office_add_l1, office_add_l2, office_city, office_state, office_pincode, dob, unverified_income, pan_number, occupation_type from contacts limit 10'
  11.     cursor.execute(query)
  12.     contacts = [[str(v) for v in value] for value in cursor.fetchall()]
  13.  
  14.     for contact in contacts:
  15.  
  16.         query = 'select phone_number in phone_numbers where contact_id={0} limit 3'.format(contact[0])
  17.         cursor.execute(query)
  18.         phone_numbers = [str(number) for number in cursor.fetchall()]
  19.         for i in range(3):
  20.             try:
  21.                 phone_numbers[i]
  22.             except IndexError:
  23.                 phone_numbers[i] = ''
  24.  
  25.  
  26.         query = 'select email in phone_numbers where contact_id={0} limit 3'.format(contact[0])
  27.         cursor.execute(query)
  28.         emails = [email for email in cursor.fetchall()]
  29.         for i in range(3):
  30.             try:
  31.                 emails[i]
  32.             except IndexError:
  33.                 emails[i] = ''
  34.  
  35.         file.write(','.join(contact + phone_numbers + emails) + '\n')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top