I need a small tool to print a record layout with a row of data to proof the layout and data match. To that end, I am using a CSV to pick up a record, and a list to store the field names.

What I want to do is to pick up a field name, and a field of data and combine them to print; "Field:data".

I thought looping through the list, then sub looping through the record would work

for item in list:
    for field in record:
        print item + ': ' + field

This does not work, it produces 'Item: field field field filed' becasue it completes the sub loop first before returning to the item list.

I was thinking of feeding both to a key pair. Anyone have an obvious idea that's ecscaping me?

Recommended Answers

All 5 Replies

Use zip

pairs = zip(itemlist, recordlist)

Use zip

pairs = zip(itemlist, recordlist)

I must be using it wrong, I've tried printing the pair, and turning the pair into a list. I don't think the CSV record is compatable? The pair holds:

[('Field', '# Name: module1')]

in the first pass, and

[('Field', '\t\tDATA\t\tDATA\t\t\tDATA\tDATA\tDATA')]

on the second, Assigning the entire record to the right side of the pair.

I must be using it wrong, I've tried printing the pair, and turning the pair into a list. I don't think the CSV record is compatable? The pair holds:

[('Field', '# Name: module1')]

in the first pass, and

[('Field', '\t\tDATA\t\tDATA\t\t\tDATA\tDATA\tDATA')]

on the second, Assigning the entire record to the right side of the pair.

Can you post an example of your data, and the expected output ?

Do you want to take each record and print it as a column under the heading?

Thanks for everyones help. I found a way an this works great..

try:
        r = linecache.getline('Customerfile.txt', 2) ## skip header
        s = r.split('\t')

        l = ('CustomerID', 'Title', 'FirstName', 'MiddleInitial', 'Lastname', 'Suffix', 'CompanyName', 'Address', 'City', 'State', 'Zip')

        for i in range(len(l)-1):
            print l[i]+':  '+s[i]

    except: ## This is here on purpose with no catcher - Simulates on error resume next.
        print ''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.