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
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(). - "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
- 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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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....
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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++...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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,...
| |