tags:

views:

102

answers:

4

i just installed python

i am trying to run this script:

import csv
reader = csv.reader(open("some.csv", "rb"))
for row in reader:
    print row

i am running on windows.

  1. do i have to type each line individually into python shell or can i save this code into a text file and then run it from the shell?
  2. where does some.csv have to be in order to run it? in the same c:\python26 folder?
  3. what is this code supposed to do?
+1  A: 
  1. Type the code into a *.py file, and then execute it.
  2. I think the file should be in the same folder as your *.py script.
  3. This opens a file stored in comma separated value format and prints the contents of each row.
recursive
where is the py script?
I__
+3  A: 
  1. Yes, you can create a file. The interactive shell is only for learning syntax, etc., and toying with ideas. It's not for writing programs.

    a. Note that the script must have a .py extension, e.g., csvprint.py. To run it, you enter python csvprint.py. This will try to load csvprint.py from the current directory and run it.

  2. The some.csv file has to be in the current working directory, which doesn't have to be (in fact, almost never should be) in the Python folder. Usually this will be you home directory, or some kind of working area that you setup, like C:\work. It's entirely up to you, though.

  3. Without knowing the csv module that well myself, I'm guessing it reads CSV separated values from the file as tuples and prints each one out on the console.

One final note: The usual way to write such logic is to take the input from the command-line rather than hard-coding it. Like so:

import csv
reader = csv.reader(open(sys.argv[1], "rb"))
for row in reader:
    print row

And run it like so:

python csvprint.py some.csv

In this case you can put some.csv anywhere:

python csvprint.py C:\stuff\csvfiles\some.csv
Marcelo Cantos
Re: 1a: Python itself doesn't care what the file extension is, although I'm not sure if Windows does.
David Zaslavsky
what does "rb" stand for?
I__
btw i am getting sys is not defined
I__
you need to import sys at the top for this solution.
recursive
Traceback (most recent call last): File "C:\pythonwork\csvread.py", line 3, in <module> reader = csv.reader(open(sys.argv[1], "rb"))IndexError: list index out of range
I__
i did import sys
I__
+1  A: 
  1. You can do both! To run the code from a text file (such as 'csvread.py', but the extension doesn't matter), type: python csvread.py at the command prompt. Make sure your PATH is set to include the Python installation directory.

  2. "some.csv" needs to be in the current directory.

  3. This code opens a Python file descriptor specifically designed to read CSVs. The reader file descriptor then prints out each row of the CSV in order. Check the documentation out for a more detailed example: http://docs.python.org/library/csv.html

VMDX
thank you very much, but how do i run a script that is in a different folder from the shell?
I__
+2  A: 

When you have IDLE open, click File > New Window. (Or hit Ctrl + N)

This opens up a new window for you that's basically just a text editor with Python syntax highlighting. This is where you can write a program and save it. To execute it quickly, hit F5.

Sean O'Hollaren
thank you very much, but how do i run a script that is in a different folder from the shell?
I__
To open up and run a script in IDLE, you can go to the folder it's in and Right click > "Edit with IDLE". That will open it up in the text editor as if you had typed it like with the first example.
Sean O'Hollaren