access file from desktop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragrid
    New Member
    • Jan 2009
    • 29

    access file from desktop

    Hi All,

    trying to learn python via web and books
    I have Python 2.6.1 on a vista machine ......
    Can someone advise me how to code pulling in an excel file from my desktop
    I am going to try and write some code to sort on a certain string and parse the file
    I know I need to convert the file in a CSV file


    Thanks in advance
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You can read cells in an Excel file using win32com. However, I would suggest saving a CSV file and using Python module csv to read the CSV file and save the data in a form that is easy to parse. Following is a small part of a CSV file and example code using csv.DictReader( ).

    Code:
    "07 May 2008  10:55:27"
    "Advance Bill of Material Report Page  1"
    "Steel Grade ..................... A992"
    "Member Type ..................... BEAMS"
     , , , , , Total
     Ln, Qty, Material, Length, Remarks, Weight
    1,1, W30x191, 17- 3, ,3295
    2, , , , , 
    3,1, W30x173, 35- 0, ,6055
    4, , , , , 
    5,1, W30x99, 29- 3, ,2896
    
    "07 May 2008  10:55:27"
    "Advance Bill of Material Report Page  2"
    "Steel Grade ..................... A992"
    "Member Type ..................... BEAMS"
     , , , , , Total
     Ln, Qty, Material, Length, Remarks, Weight
    1,1, W24x84, 21- 0, ,1764
    2,1, W24x84, 19- 0, ,1596
    3,1, W24x84, 17- 6, ,1470
    4, , , , , 
    5,1, W24x76, 37- 9, ,2869
    Code:
    import csv
    
    f = open('example.csv')
    
    labels = ['Ln', 'Qty', 'Material', 'Length', 'Remarks', 'Weight']
    
    reader = csv.DictReader(f, labels)
    
    readerDict = {}
    
    for item in reader:
        try:
            if item['Ln'].startswith('Advance Bill of Material Report'):
                page = int(item['Ln'].split()[-1])
            else:
                line = int(item['Ln'])
                if item['Material'] != ' ':
                    readerDict.setdefault(page, []).append(item)
        except:
            pass
    
    pages = readerDict.keys()
    
    for page in pages:
        print "\nPage", page
        for line in readerDict[page]:
            print '    ', ''.join([line[key].center(fw) \
                                   for fw, key in [[4,'Ln'],
                                                   [4,'Qty'],
                                                   [20,'Material'],
                                                   [12,'Length'],
                                                   [12,'Weight'],
                                                   [24,'Remarks']]])
    
    f.close()
    -BV

    Comment

    • dragrid
      New Member
      • Jan 2009
      • 29

      #3
      access file from desktop

      Thanks BV for your reply

      However as a newbie I am trying to grasp this dictionary concept - I need to work on it more but in the mean time - can you or someone else who sees this reply - put some comments around readerDict {} - looks like you are seeting up a dictionary variable , why and also comments around the for loops - I am loosing track through some stages of the 2 for loops

      That should do it for this original request and will close afterwards


      Thanks again
      Dragrid

      Comment

      Working...