473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

access file from desktop

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
2 1813
bvdet
2,851 Expert Mod 2GB
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
dragrid
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

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

Similar topics

1
by: Mike_red | last post by:
Hello, I hope someone can solve my problem here. I am currently working/programming a website using Debian Linux + Perl + PHP + PostgreSQL + Apache. Some data in the postgreSQL server must...
1
by: Dishaa V | last post by:
Hello I need to connect to an access db which is on the internet. I do know that URL of the same. its http://www.vallury.com/balance/bank.mdb. Its just a test db. How can I connect to that db...
5
by: premmehrotra | last post by:
I currently have a multi-user access database which is put on a shared drive L: on a Windows Servers. Entire database is one file premdb.mdb. Users access this database from their laptops....
4
by: rcmail14872 | last post by:
I have MS SQL Server 2000 evaluation version on my desktop PC. I have the SQL Server client tools (only) on the laptop. The two computers are networked. I had an Access MDB database on my laptop,...
56
by: Raphi | last post by:
Hi, I've been using an Access application I wrote for an office with the front-end stored on all computers and the back-end on one of them serving as an Access file server. Now we're moving...
6
by: dbuchanan | last post by:
I have a Windows Forms application that accesses SQL Server 2k from a small local network. The application has been used for weeks on other systmes but a new install on a new machine retruns...
6
by: jonefer | last post by:
I have two versions of a 'Downtime Application that will run in the event that the mainframe goes down 1) SQL Server ASP.NET app (accessed outside the mainframe network) 2) MS Access Version of...
5
by: Jeff Richardson | last post by:
I am creating a web service that returns data from an Access 2000 database using VS2005. The Access database is located on a different server than where the web service is running. The Access...
1
by: Rob C | last post by:
I want to develop some data access web services in C# that can be accessed from both an ASP.NET web application as well as a VC++ desktop application. i have used web services in the past from VC++...
0
by: Realm Master | last post by:
I have a two computer workgroup. On my laptop I moved Access tables to SQL 2005Express and linked them. I use Access as the front end and everything works from the laptop. Both the access files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.