more newbie list questions

Bernhard Holzmayer Holzmayer.Bernhard at deadspam.com
Thu Jul 14 06:20:03 EDT 2005


googleboy wrote:

> Hi there.
Hi googleboy!
> 
> I am doing a bunch of processing over a list of lists,  and am
> interested in doing several things taht don't seem to be working for me
> just at the moment.
> 
> I have a list of books with several fields (Title, Author1, Author2,
> Publisher, ISBN) in a csv.
> 
> I have a cell.txt file that looks like this:
> 
> ++
> The title is %title%.  <br><br>
> The author is %author1% %author2% <br><br>
> The Publisher is %publisher1% %publisher2% <br><br>
> The ISBN is %ISBN% <br><br>
> ++

This looks like a DOS-batch-file. Maybe you'd better just leave it to
DOS to populate it, just by exec-uting it  in an environment which 
has title, authort1, ... set. ??

On the other hand, if you need not relate to the cell.txt file, 
you could just use something like
 
sAuth = "The author is %s" % author1

> 
> I know how to do something like sAuth = re.sub('%author1%', author1,
> sTemplate), but I can't figure out to populate variables within python
> from the list of fields.

I guess, that you open the csv-file and then use readline for each record.
Since csv files are ; separated (or ,), just do
recordlist = current_line.split(';')
which provides all entries from the read line in a list.
Or even better:
(title,author1,author2,publisher1,publisher2,ISBN) = current_line.split(';')
Now you can do something like
sAuth = "The author is %s" % recordlist[1]  in the first case, or
sAuth = "The author is %s" % author1        in the second

> 
> The second thing I am trying to figure out is how to add a field (or
> populate an empty field) called filename with the value of ISBN +
> '.txt', though I hope this becomes pretty self evident after someone
> helps me with the above.

You're certainly right about the evidence of the following:
filename="%s.txt" % ISBN

> 
> The last thing that is vexing me at the moment is something I am not
> sure if it is possible.  Once I have populated this filename field, I
> will want to try to do something like:
> 
> for book in all_books
>     open(r'd:\path\to\files\%filename%', 'a')
>     some_function
> 
> 
> Where I replace %filename% with the value of the filename field for
> each book.  Is it possible to do some sort of search and replace like
> that over a command within python?
> 
> Or is there another way entirely different to accomplish such a task,
> like maybe assigning the path/filename to a variable... ?
> 
I'd try like this:
0. Initialize an empty list   all_books=[]
1. While reading in the csv file line by line, you received the ISBN
   for that line(book). (Look above the line with the split command)
   Now add this ISBN to the list by:
        all_books.append(ISBN)

2. After the list is complete (all records read, file closed),
   you can do this as you assumed:
        for book in all_books:
                f=open(r"d:\path\to\files\%s.txt" % book, 'a')
                some_function...
                f.close()
> TIA!
> 
> Googleboy
Bernhard

P.S: 
1. Looking into the Python tutorial which is available online at
www.python.org, would have answered most of your questions.




More information about the Python-list mailing list