4 Replies - 136 Views - Last Post: 15 November 2019 - 03:44 PM Rate Topic: -----

#1 kam536   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 21-March 18

Formatting

Posted 15 November 2019 - 02:39 PM

import re

print("Hello,")
print("Welcome to the Python Data Munging Application")


userInfo=[['Monica','Simmons','20640','123-456-7890'],['Jim','Robertson',\
'21801','555-555-5555'],['John','Adams','223211143','4444444444'],\
['Helen','Cooper','edskd-2134','323232'],['','Franklin','234511','323-333-2211'\
],['Sarge','','57105','419-503-9163'],['Art','Cooper','21081','333-333-3333'],\
['123Samuel','Artisan','82130','213-219-216'],['Martin','Roberts','42631',''],\
['Josephine','Saltsman','20681','301-523-9163'],['Damien','Salvador','',\
'301-855-1234'],['Reishi','Smith','20640','240-682-0608'],['Leonardo',\
'Matthews','12345-abcd','222-222-2222'],['1539','Andrews','12345-6789',\
'240-923-8500'],['Elijah','Smith','20640','301-379-0190']]





 
def dataCheckerFName():
    for row in userInfo:
        if not row[0].isalpha():
            row[0] = ''
            print('Name', row[0])
    



def dataCheckerLName():
    for row in userInfo:
        if not row[1].isalpha():
            row[1] = ''
   
def dataCheckerZip(): 
    for row in userInfo:
        if not row[2].replace('-', '').isdigit():
            row[2] = ''
        if row[2] is not '':
            row[2] = re.sub(r'\D', '', row[2])
        if len(row[2]) == 9:
            row[2] = row[2][:5] + '-' + row[2][5:]
        elif len(row[2]) == 5:
             row[2] = row[2][:5] + '-' + row[2][5:]
        else:
            row[2] = ''
    
def dataCheckerPhone():
    for row in userInfo:
        if not row[3].replace('-', '').isdigit():
            row[3] = ''
        if row[3] is not '':
            row[3] = re.sub(r'\D', '', row[3])
        if len(row[3]) == 10:
            row[3] = row[3][:3] + '-' + row[3][3:6] + '-' + row[3][6:]
        else:
            row[3] = ''



def processUserInfo():
    dataCheckerFName()
    dataCheckerLName()
    dataCheckerZip()
    dataCheckerPhone()
    details = ['Firstname', 'Lastname', 'Zipcode', 'Phone']
    userInfo.insert(0, details)
    userInfo.insert(1, [])



def printUserInfo():
    for row in userInfo:
        for item in row:
            if item is '':
                print('\t ', end=" ")
            else:
                print(item, end=" ")
                
    print()
    
    
    
    
    
    
def main():
    processUserInfo()
    printUserInfo()



if __name__ == '__main__':
    main()
    print(len(userInfo))




This is the output:
Hello,
Welcome to the Python Data Munging Application
Name
Name
Name
Firstname Lastname Zipcode Phone Monica Simmons 20640- 123-456-7890 Jim Robertson 21801- 555-555-5555 John Adams 22321-1143 444-444-4444 Helen Cooper Franklin 323-333-2211 Sarge 57105- 419-503-9163 Art Cooper 21081- 333-333-3333 Artisan 82130- Martin Roberts 42631- Josephine Saltsman 20681- 301-523-9163 Damien Salvador 301-855-1234 Reishi Smith 20640- 240-682-0608 Leonardo Matthews 222-222-2222 Andrews 12345-6789 240-923-8500 Elijah Smith 20640- 301-379-0190
17


I'm trying to format this to print everything out in more of a chart, what could I use to do that?

Is This A Good Question/Topic? 0
  • +

Replies To: Formatting

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,599
  • Joined: 12-June 08

Re: Formatting

Posted 15 November 2019 - 02:46 PM

Seems like you are missing new line characters being printed out.
Was This Post Helpful? 0
  • +
  • -

#3 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,794
  • Joined: 27-December 13

Re: Formatting

Posted 15 November 2019 - 03:31 PM

If you first delete line 69 (because it inserts an empty list), you can do:
def printUserInfo():
    my_format = "{:>20}"*4
    for row in userInfo:
        print(my_format.format(*row))
    


This rely on the python format mini-language, documented here: https://docs.python....n-mini-language
Was This Post Helpful? 0
  • +
  • -

#4 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,794
  • Joined: 27-December 13

Re: Formatting

Posted 15 November 2019 - 03:37 PM

Further:

If you want the empty line right after the headings, you can do:
def printUserInfo():
    for row in userInfo:
        for field in row:
            print('{:>20}'.format(field), end = " ")
        print(" ")

To learn about the many possibilities, you need to look-up the link above.
Was This Post Helpful? 0
  • +
  • -

#5 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 561
  • View blog
  • Posts: 1,794
  • Joined: 27-December 13

Re: Formatting

Posted 15 November 2019 - 03:44 PM

Oh, one more thing.
To avoid a trailing hyphen, '-', in 5-digit zip codes, just change line 45 to pass
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1