By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,025 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

How to read data into an array

ryno du preez
P: 91
HI all
I'm reading data from a CSV (text file) with a "| " as the denominator. Every is working as planned but I'm having a problem with the last 3 lines of code. In the text Document the 'RB' (Body) has more than one entry exp.
RB | INV000111 | price to be charged
RB | INV000112 | price 2 to be charged

some has more then TWO entry. Now the problem being

When Python reads the CSV it only writes the Second line to the PDF INV000112 not both lines as it is displayed.

How can I get it to write Both lines as displayed in the text file


Expand|Select|Wrap|Line Numbers
  1. db = csv.reader(open('C:/Users/rynod/Desktop/cpc.txt','rb'), delimiter = '|', quotechar='"')
  2.     for row in db:
  3.         for field in enumerate(row):
  4.             if 'H' in field:
  5.                #Customer Details
  6.                CustPAName = row[9]
  7.                CustPA1 = row[10]
  8.                CustPA2 = row[11]
  9.                CustPA3 = row[12]
  10.                CustPA4 = row[13]
  11.                CustPA5 = row[14]
  12.                CustFAName = row[18]
  13.                CustFA1 = row[19]
  14.                CustFA2 = row[20]
  15.                CustFA3 = row[21]
  16.                CustFA4 = row[22]
  17.                CustEMail = row[15]
  18.                #Sharp Details
  19.                SharpAddress1 = row[2]
  20.                SharpAddress2 = row[3]
  21.                SharpAddress3 = row[4]
  22.                SharpAddress4 = row[5]
  23.                SharpTel = row[6]
  24.                SharpFax = row[7]
  25.                Model = row[16]
  26.                Serial = row[17]
  27.                Account = row[25]
  28.                OurRef = row[27]
  29.             elif 'RB' in field:
  30.                print(row)
  31.                Decs = row 
Mar 25 '14 #1

✓ answered by dwblas

Your first problem is using enumerate
Expand|Select|Wrap|Line Numbers
  1. row = ["ZHZ", "AH", "HA"]
  2. for field in enumerate(row):
  3.     print field, "--> H",
  4.     if 'H' in field:
  5.         print "foumd"             
  6.     else:
  7.         print "     NOT found" [
You don't use it so if we take it out
Expand|Select|Wrap|Line Numbers
  1. row = ["ZHZ", "AH", "HA"]
  2. for field in row:
  3.     print field, "--> H",
  4.     if 'H' in field:
  5.         print "foumd"             
  6.     else:
  7.         print "     NOT found" 
but this will find any "H" anywhere in the record. Is that what you want to do? Some test data would help.

use .join() to convert a list to a string
Expand|Select|Wrap|Line Numbers
  1. print("*".join(data[1:2]))
but data[1:2] == data[1] so you can just print data[1] in this specific case.

Every is working as planned but I'm having a problem with the last 3 lines of code. In the text Document the 'RB' (Body) has more than one entry exp.
RB | INV000111 | price to be charged
RB | INV000112 | price 2 to be charged
Using csv is overkill IMHO in this case. Just read the file and split each record.
Expand|Select|Wrap|Line Numbers
  1. """
  2. row = ["ZHZ", "AH", "HA"]
  3. """ commented out because "test_it" is used to simulate the file
  4. with open(file_name, "r") as fp:  ## replaced by test_it
  5.     for rec in fp:                ## replaced by for rec in test_it
  6. """
  7. test_it= ['ABC | INV001 | price',
  8.           'HB | INV000111 | price to be charged',
  9.           'RB | INV000112 | price 2 to be charged']
  10.  
  11. for rec in test_it:
  12.     data = rec.split("|")
  13.     print data[0], "--> HB",
  14.     if 'HB' == data[0].strip(): ## strip white space in file
  15.         print "foumd"             
  16.     else:
  17.         print "     NOT found" 
And also I don't thing this is very efficient because it will break if I have more then 2 entry
Write each entry to the pdf file as it is found, then it doesn't matter how many there are.

Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Why do you need to do a nested for loop? You should be able to do it in one loop. Why not compile a dictionary instead of assigning so many variables? It would work something like the following where data is a line in db
Expand|Select|Wrap|Line Numbers
  1. fields = ['SharpAddress1',
  2.           'SharpAddress2',
  3.           'SharpAddress3',
  4.           'SharpAddress4',
  5.           'SharpTel',
  6.           'SharpFax']
  7.  
  8. data = [0,1,2,3,4,5,6,7,8,9,10]
  9.  
  10. dd = dict(zip(fields, data))
I am not sure why only one "RB" line is printed other than it has something to do with the nested loop or your use of enumerate. Could you post a representative portion of the CSV file?
Mar 25 '14 #2

ryno du preez
P: 91
I'm trying to write a report in python where the data is read from a text file with CSV and then using report lab producing a PDF file(Invoice)

In the text file We have different sections 'H' Header 'B' Body ect. so that is why I'm using more then one loop. it is reading the text file and using row to assign it to a verable that gets coped to the PDF.
I manage to get it to read the RB section by using the following
Expand|Select|Wrap|Line Numbers
  1.     data = []
  2.             for i in range(1):   
  3.                 if 'RB' in field:
  4.                    data.append(row[3])
  5.                    print(data[1:2])
  6.                 cal = str(data[0:1])
  7.                 cal2 = str(data[1:2])
But the problem is now it shows it as a list as a string with the '[]' how do I get the '[]' not to show.

['546 @ 0.0879 cents = R47.99']
['165 @ 0.0967 cents = R15.96']

And also I don't thing this is very efficient because it will break if I have more then 2 entry

And as you probably guest I'm also just started learning python.
Thanks for your help so far
Mar 25 '14 #3

Expert 100+
P: 614
Your first problem is using enumerate
Expand|Select|Wrap|Line Numbers
  1. row = ["ZHZ", "AH", "HA"]
  2. for field in enumerate(row):
  3.     print field, "--> H",
  4.     if 'H' in field:
  5.         print "foumd"             
  6.     else:
  7.         print "     NOT found" [
You don't use it so if we take it out
Expand|Select|Wrap|Line Numbers
  1. row = ["ZHZ", "AH", "HA"]
  2. for field in row:
  3.     print field, "--> H",
  4.     if 'H' in field:
  5.         print "foumd"             
  6.     else:
  7.         print "     NOT found" 
but this will find any "H" anywhere in the record. Is that what you want to do? Some test data would help.

use .join() to convert a list to a string
Expand|Select|Wrap|Line Numbers
  1. print("*".join(data[1:2]))
but data[1:2] == data[1] so you can just print data[1] in this specific case.

Every is working as planned but I'm having a problem with the last 3 lines of code. In the text Document the 'RB' (Body) has more than one entry exp.
RB | INV000111 | price to be charged
RB | INV000112 | price 2 to be charged
Using csv is overkill IMHO in this case. Just read the file and split each record.
Expand|Select|Wrap|Line Numbers
  1. """
  2. row = ["ZHZ", "AH", "HA"]
  3. """ commented out because "test_it" is used to simulate the file
  4. with open(file_name, "r") as fp:  ## replaced by test_it
  5.     for rec in fp:                ## replaced by for rec in test_it
  6. """
  7. test_it= ['ABC | INV001 | price',
  8.           'HB | INV000111 | price to be charged',
  9.           'RB | INV000112 | price 2 to be charged']
  10.  
  11. for rec in test_it:
  12.     data = rec.split("|")
  13.     print data[0], "--> HB",
  14.     if 'HB' == data[0].strip(): ## strip white space in file
  15.         print "foumd"             
  16.     else:
  17.         print "     NOT found" 
And also I don't thing this is very efficient because it will break if I have more then 2 entry
Write each entry to the pdf file as it is found, then it doesn't matter how many there are.
Mar 25 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.