Reading CSV files in IronPython

- Image via Wikipedia
This is in continuation with my previous blog post :
To get IronPython to use Standard Python Modules, one needs to add the following two lines to C:\IronPython-2.0.1\Lib\site.py :
import sys sys.path.append(r"C:\Python25\Lib")While this works for most part, it doesn’t help if you are using Python extensions written in C. More about my specific problems in another post. But there is an open source project IronClad to deal specifically with this issue. In the meantime, you can check the differences between IronPython and CPython
Reading (and writing to) CSV file is critical part of my program, while in stadard python it was as easy as “import csv”, the same thing took some efforts to get it working in IronPython. I got the following error for my import statement
Error on line 7 in csv.py from functools import reduce
I also tried using ActiveState Python 2.5.2.2 (which I already had from few months ago, didn’t feel like downloading the latest version till the problem was fixed) But that didn’t help either. With ActiveState, I got the same error on the same line, except this time it was for _csv.
To Quote from IronPython Cookbook :
For some reason the Python standard library csv module is written in C, which means that it isn’t available to IronPython.
The cookbook points to a third party library called A Fast Csv Reader . The cookbook has a nice example of how to use the said DLL with your IronPython Program.
It wasn’t clear to me as to why I had to register at Code Project to download this binary since it is provided under MIT Open Source License. But whom am I gonna complain to ? Beggars can’t be choosers
IronPython
by Mandar Vaze on June 23, 2009
in Python, Review, windows

- Image by Michael Foord via Flickr
After working on initial prototype using pylons, on ubuntu, my employer needed a standalone application (on windows) So I was looking for ways to reuse my python code to avoid double work. had only heard about iron python but beyond that I did not know anything more.
A bit of research later, I downloaded IronPython from here and IronPython Studio. I was quite thrilled to find out that Microsoft, has created this shell, which allows developers to integrate new languages with Visual Studio Shell.
Installing IronPython Studio
Installing IronPython was very straight forward. Considering it is one of the Python implementation itself, you don’t have to have standard python installed. (But I did, more on that later)
Setting up IronPython Studio wasn’t so straight forward. I had to download standalone version of Visual Studio shell. But the problem, and it is well documented, is that installing the redistributable package in itself is not the complete step, as you would believe from the website. I confirmed that the package was installed, and stil IronPython Studio would complain, and refuse to proceed. It turns out that you need to install the Visual Studio environment, which is inside this directory structure you just created.
This is NOT enough
This is what IronPython Studio needs
Once I installed Visual Studio Shell, IronPython Studio installed easily. Using IronPython has been nice experience in just a few hours I have been using it. The Code completion isn’t upto my liking yet, but it is helpful at times.
Using Standard Python Libraries
Since my original code was written on linux, it used only the standard python modules. In a manner of speaking, it was “pure” python code. Since IronPython is an implementation of the Python programming language running under .NET, it was obivious that my code may not work as is. But I’m glad that I did not have to make too many changes to get it working under IronPython.
As is well documented in IronPython Tutorial, it is not very difficult to use standard python libraries with IronPython (with a few exceptions – more about this a little later) To get IronPython to use Standard Python Modules, one needs to add the following two lines to C:\IronPython-2.0.1\Lib\site.py :
import sys sys.path.append(r"C:\Python25\Lib")
While this works for most part, it doesn’t help if you are using Python extensions written in C. More about my specific problems in another post. But there is an open source project IronClad to deal specifically with this issue. In the meantime, you can check the differences between IronPython and CPython
Related articles
- IronPython in Action (blogs.msdn.com)
- Why Python? (mehmetalierturk.com)
Access your todo list from multiple locations
by Mandar Vaze on January 17, 2009
in Hack, Open Source, Productivity, Python, Tutorials
Here is a clever method to access your todo list from multiple locations.
Ingredients
- todo.sh – This is a command line shell scripts which allows to manage your TO DO list
- DropBox Account : Free, Syncs various computers, Cross Platform (at least works on Windows as well as Linux)
Recipe
- Make Sure you have installed Dropbox clients on all of your machines (Mainly Work and Home)
- Download and install todo.sh in your Dropbox folder. This is main trick to make your TODO list portable.
- You need to modify the .todo file (or todo.py script itself) to set the TODO_DIR variable. I’ve created a folder called “tododir” in my Dropbox folder, and set the TODO_DIR variable as “tododir”. This helps avoiding path problems on various machines.
- (Optional) Read the tutorial available at LifeHacker
- Open command Prompt, and take charge of your TODO lists.
Why This Idea Rocks
- Since the todo list is maintained as simple text file, any machine/platform allows you to directly view and edit your todo list.
- While one would install Dropbox clients on all of their regularly used machines, You can always view and edit your todo.txt even from a cyber cafe, using Dropbox’s web interface.
Are you on Windows ?
- If you are on Windows, you will require cygwin which provides unix utilities on windows. (I was unable to get todo.sh working with mSys, which I got as part of mSys Git (More about it later, in another post))
- There is a variation of todo.sh written in Python (which is what I’m using these days). If which case, you need Python instead of Cygwin. Python version supports color coding of the items based on priority even on Windows.
Can I not just use USB Drive instead ?
- Sure you can. But some employers do not allow the employees to connect their personal USB devices to office computers. (Security, Virus threats) in which case Dropbox is better alternative.
- Even Cyber cafe won’t allow to plug in your USB drive, but you sure can access your files from Web Front end of Dropbox.
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_b.png?x-id=97b6f3b5-f068-4b14-accd-9a79a802c586)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_b.png?x-id=d57dd1e2-9cff-474f-9b2e-8f575bf5d8b3)

