last day (14 days later) » 

5:58 PM
0
A: How can I select only a particular row in a CSV file?

martineauAssuming you count rows from 1 (rather than 0), here's a standalone function that will do it: import csv from contextlib import contextmanager import sys import itertools @contextmanager def multi_file_manager(files, mode='r'): """ Context manager for multiple files. """ files = [open(f...

 
This script generates an error message: builtins.execfile(filename, *where) File "/home/fred/pagekicker-community/scripts/bin/csv_row_reader.py", line 8 def multi_file_manager(*files, mode='r'): ^ SyntaxError: invalid syntax I don't understand how the mode is defined. It looks as if it's defined as r in the function but then called as w in the script. Why isn't it just defined as 'mode'?
 
Sorry about that—I modified something I wrote long ago and didn't test the changes and have updated the code in my answer to address the issue.
To answer your question about how mode is defined: You can give arguments default values in the function definition by assigning a value to them using arg=x syntax. So in the case the files will all be opened in read-mode by default. However, when the function is actually called, the default value can be overridden by supplying a different value for it. Conversely, if a function is defined with default arguments, they can left out of calls to it in which case the defaults will be used.
 
` builtins.execfile(filename, *where) File "/home/fred/pagekicker-community/scripts/bin/csv_row_reader.py", line 36, in <module> with multi_file_manager(destinations, mode='w') as files: File "/usr/lib/python2.7/contextlib.py", line 17, in enter return self.gen.next() File "/home/fred/pagekicker-community/scripts/bin/csv_row_reader.py", line 10, in multi_file_manager files = [open(file, mode) for file in files] IOError: [Errno 2] No such file or directory: '/csv/row.editedby'` I don't get this, the /tmp/pagekicker/123/csv is writable
 
If the directory exists and there's no write-permissions issue, it sounds it might be attempting to open the file in read mode (although that shouldn't be the case).
 
destinations = [os.path.join(folder, dest) for dest in ('/csv/row.editedby', '/csv/row.booktitle', '/csv/row.seeds', '/csv/row.imprint')] print destinations >>> runfile('/home/fred/pagekicker-community/scripts/bin/csv_row_reader.py', args='/home/fred/scratch/1001_7.csv 123 2') ['/csv/row.editedby', '/csv/row.booktitle', '/csv/row.seeds', '/csv/row.imprint'] >>>
priin destinations seems to be only generating /csv/* not the full path
 
6:01 PM
What's that runfile stuff?
Hello?
 
I am using Spyder which allows you to configure cmldline parameters for the console interpreter. but I get same result when I run file from command line.
I did "print destiinations' to see if it was generating the file names correctly
it looks as if the destinations are /csv/row.* instead of /tmp/pagekicker/123/csv/row.*

Could there be anything wrong with

destinations = [os.path.join(folder, dest) for dest in
 
It doesn't look like the file names have had the contents of folder prefixed to them, which is what the os.path.join() is doing. Are you sure folder contains the right string value?
Yes, there appears to be something wrong with the
destinations = [os.path.join(folder, dest) for dest in
 
print folder yields /tmp/pagekicker/123. I replaced "folder" in join(folder, dest) with the hard code path ('/tmp/...123/) and it still yields /csv/.. not /tmp/...
 
6:16 PM
Yes, the os.path.join() isn't doing what I thought -- don't know why yet.
The problem is both folder and the /csv/xxxx string start with a '/', so join considers them both absolute paths (and ignores the first apparently). The simplest fix is to use this instead:
destinations = [folder+dest for dest in ('/csv/row.editedby',
'/csv/row.booktitle',
'/csv/row.seeds',
'/csv/row.imprint')]
 
works like a champ. thanks so much!
 
Sure thing. Sorry for having to iterate in on the answer. Usually I test code before I post it, but didn't in this case. My bad.
Going to leave chat now. OK?
Guess so. Bye.
 

last day (14 days later) »