How to read data into an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ryno du preez
    New Member
    • Jul 2011
    • 91

    How to read data into an array

    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


    Code:
    db = csv.reader(open('C:/Users/rynod/Desktop/cpc.txt','rb'), delimiter = '|', quotechar='"')
        for row in db:
            for field in enumerate(row):
                if 'H' in field:
                   #Customer Details
                   CustPAName = row[9]
                   CustPA1 = row[10]
                   CustPA2 = row[11]
                   CustPA3 = row[12]
                   CustPA4 = row[13]
                   CustPA5 = row[14]
                   CustFAName = row[18]
                   CustFA1 = row[19]
                   CustFA2 = row[20]
                   CustFA3 = row[21]
                   CustFA4 = row[22]
                   CustEMail = row[15]
                   #Sharp Details
                   SharpAddress1 = row[2]
                   SharpAddress2 = row[3]
                   SharpAddress3 = row[4]
                   SharpAddress4 = row[5]
                   SharpTel = row[6]
                   SharpFax = row[7]
                   Model = row[16]
                   Serial = row[17]
                   Account = row[25]
                   OurRef = row[27]
                [B]elif 'RB' in field:
                   print(row)
                   Decs = row[/B]
    Last edited by bvdet; Mar 25 '14, 03:06 PM. Reason: Clarify title
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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
    Code:
    fields = ['SharpAddress1',
              'SharpAddress2',
              'SharpAddress3',
              'SharpAddress4',
              'SharpTel',
              'SharpFax']
    
    data = [0,1,2,3,4,5,6,7,8,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?

    Comment

    • ryno du preez
      New Member
      • Jul 2011
      • 91

      #3
      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
      Code:
          data = []
                  for i in range(1):   
                      if 'RB' in field:
                         data.append(row[3])
                         print(data[1:2])
                      cal = str(data[0:1])
                      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

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Your first problem is using enumerate
        Code:
        row = ["ZHZ", "AH", "HA"]
        for field in enumerate(row):
            print field, "--> H",
            if 'H' in field:
                print "foumd"             
            else:
                print "     NOT found" [
        You don't use it so if we take it out
        Code:
        row = ["ZHZ", "AH", "HA"]
        for field in row:
            print field, "--> H",
            if 'H' in field:
                print "foumd"             
            else:
                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
        Code:
        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.
        Code:
        """
        row = ["ZHZ", "AH", "HA"]
        """ commented out because "test_it" is used to simulate the file
        with open(file_name, "r") as fp:  ## replaced by test_it
            for rec in fp:                ## replaced by for rec in test_it
        """
        test_it= ['ABC | INV001 | price',
                  'HB | INV000111 | price to be charged',
                  'RB | INV000112 | price 2 to be charged']
        
        for rec in test_it:
            data = rec.split("|")
            print data[0], "--> HB",
            if 'HB' == data[0].strip(): ## strip white space in file
                print "foumd"             
            else:
                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.

        Comment

        Working...