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

access file from desktop

P: 29
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
Feb 4 '09 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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().

Expand|Select|Wrap|Line Numbers
  1. "07 May 2008  10:55:27"
  2. "Advance Bill of Material Report Page  1"
  3. "Steel Grade ..................... A992"
  4. "Member Type ..................... BEAMS"
  5.  , , , , , Total
  6.  Ln, Qty, Material, Length, Remarks, Weight
  7. 1,1, W30x191, 17- 3, ,3295
  8. 2, , , , , 
  9. 3,1, W30x173, 35- 0, ,6055
  10. 4, , , , , 
  11. 5,1, W30x99, 29- 3, ,2896
  12.  
  13. "07 May 2008  10:55:27"
  14. "Advance Bill of Material Report Page  2"
  15. "Steel Grade ..................... A992"
  16. "Member Type ..................... BEAMS"
  17.  , , , , , Total
  18.  Ln, Qty, Material, Length, Remarks, Weight
  19. 1,1, W24x84, 21- 0, ,1764
  20. 2,1, W24x84, 19- 0, ,1596
  21. 3,1, W24x84, 17- 6, ,1470
  22. 4, , , , , 
  23. 5,1, W24x76, 37- 9, ,2869
Expand|Select|Wrap|Line Numbers
  1. import csv
  2.  
  3. f = open('example.csv')
  4.  
  5. labels = ['Ln', 'Qty', 'Material', 'Length', 'Remarks', 'Weight']
  6.  
  7. reader = csv.DictReader(f, labels)
  8.  
  9. readerDict = {}
  10.  
  11. for item in reader:
  12.     try:
  13.         if item['Ln'].startswith('Advance Bill of Material Report'):
  14.             page = int(item['Ln'].split()[-1])
  15.         else:
  16.             line = int(item['Ln'])
  17.             if item['Material'] != ' ':
  18.                 readerDict.setdefault(page, []).append(item)
  19.     except:
  20.         pass
  21.  
  22. pages = readerDict.keys()
  23.  
  24. for page in pages:
  25.     print "\nPage", page
  26.     for line in readerDict[page]:
  27.         print '    ', ''.join([line[key].center(fw) \
  28.                                for fw, key in [[4,'Ln'],
  29.                                                [4,'Qty'],
  30.                                                [20,'Material'],
  31.                                                [12,'Length'],
  32.                                                [12,'Weight'],
  33.                                                [24,'Remarks']]])
  34.  
  35. f.close()
-BV
Feb 4 '09 #2

P: 29
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
Feb 6 '09 #3

Post your reply

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