0

Good Afternoon, Python Newb here. I'm trying to return a list of customer information that has been scraped from a text file. I need the output to be in a Name, Account Number, Date format. My initial though thought is,

scrape data create list Print by index_number e.g. (Name 1, Account Number 1, Date 1)

Unfortunately, this doesn't work because the list will print out with all the names, then all the Account Numbers, then the date(s). I need the list to print out as name, account number, date.

I'm pretty sure this is because of the way I have the loop running. Below is the code I have been working on.

   import re

fin = open(destFileLoc,"r")
text = fin.read()


nameMatch = re.findall(r'\n\w+\s+\w+\s\w+', text)
# for i in range(len(nameMatch)):
#     name = nameMatch # print("Name: " + nameMatch[i])

acctMatch = re.findall(r'\s{4}\d{8}', text)
# for i in range(len(acctMatch)):
#     account = acctMatch  ##print("Account Num: " + acctMatch[i])

dateMatch = re.findall(r'(\d+/\d+/\d+)', text)
# for i in range(len(dateMatch)):
#     date = dateMatch  ## print("Date of Service : " + dateMatch[i])

patList = [[nameMatch], [acctMatch], [dateMatch]]
for i in range(patList):
    print("====== Name     Account Number       Date ======\n" + str(nameMatch[i]), str(acctMatch[i]), str(dateMatch[i]))
1
  • The "zip" function can help to reorganize the lists into one list of tuples which can then be printed. Commented Oct 14, 2019 at 19:37

2 Answers 2

1

you could try using zip which combines multiple lists

for name, acctNum, date in zip(nameMatch, acctMatch, dateMatch):
    print(str(name), str(acctNum), str(date))
Sign up to request clarification or add additional context in comments.

Comments

0

Try ...

See also The format module

nameMatch =["Smith", "Jones", "Thompson"]
acctMatch =["12345", "54321", "22333"]
dateMatch =["2019-10-1", "2019-10-2", "2019-10-3"]

a = list(zip(
    nameMatch,
    acctMatch, 
    dateMatch
))

print(a)

for _list in a:
    print("=========================")
    print("Printing the list itself: ", _list)
    print ('Printing the items in _list:', _list[0], _list[1], _list[2])

print("=========================")
print ('Printing the items with format and columns:')
for _list in a:
    print( '{0:.<10}{1:.<10}{2:.<10}'.format(_list[0], _list[1], _list[2]))

OUTPUT:

[('Smith', '12345', '2019-10-1'), ('Jones', '54321', '2019-10-2'), ('Thompson', '22333', '2019-10-3')]
=========================
Printing the list itself:  ('Smith', '12345', '2019-10-1')
Printing the items in _list: Smith 12345 2019-10-1
=========================
Printing the list itself:  ('Jones', '54321', '2019-10-2')
Printing the items in _list: Jones 54321 2019-10-2
=========================
Printing the list itself:  ('Thompson', '22333', '2019-10-3')
Printing the items in _list: Thompson 22333 2019-10-3
=========================
Printing the items with format and columns:
Smith.....12345.....2019-10-1.
Jones.....54321.....2019-10-2.
Thompson..22333.....2019-10-3.

5 Comments

This looks like it works. I still need to validate the data, but thank you for the suggestion. Also, is there a way to a return (next line) and strip the single quotes when it print() (prints)? so it looks like, Name1 Acct1 Date1 Name2 Acct2 Date2 Name3 Acct3 Date3 etc. Thanks again for the help!
I believe I figured out the new line for each row. print('\n'.join(map(str, patList))) This returns what I would like to see, however, I need to strip out the single quotes still.
I think you're seeing the quotes because you're printing the list object itself, versus the items in the list. I'll updated my answer to show different printing options.
This looks great! Thanks, I appreciate the help!
My pleasure. If it helped, don't forget to mark it as the answer, and/or add an upvote :) Stack Exchange encourages this for any post that has been helpful (even if it's just one you searched for, not a question you asked :) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.