more newbie list questions

googleboy mynews44 at yahoo.com
Thu Jul 14 11:53:36 EDT 2005


Ali wrote:
> It's not really clear what you mean?

Ah.  sorry.  Let me try again.

I start with a csv that looks something like this:

title, author1, author2, publisher, code, ISBN
frogs of canada, andy humber, , springer press, foc2, 111-20345-556
newts of the UK, nigel snodgrass, sarah strauss, cambridge press,
nsss23,32-443567-130
amphibian environments, nigel snodgrass, , cambridge press, nsxx11,
32443567-120

First problem:

I have a seed file which I have placed a set of easily searchable
keywords (%author1%, %publisher% etc.) in.  I want to be able to
replace these keywords with the relevent values from each book, in
order to create an individual text file for each book called code.txt
(where the word code is replaced by the code of each book)

I've read the tutorial and (I think I)know how to use regular
expressions to make the substitions, but I can't figure out how to get
to the individual values of each book.

I have been trying the following:

++
import string, os, re, sys, operator, csv

class Book(object):
    def __init__(self, title, author1, author2, publisher, code,ISBN):
        params = locals()
        del params['self']
        self.__dict__.update(params)
    def __repr__(self):
        all_items = self.__dict__.items()
        return str(all_items)

def read_books(filename):
    reader = csv.reader(csv_file)
    books = [Book(*[field.strip() for field in row]) for row in reader]
    csv_file.close()
    return books

booklist = read_books("c:\path\to\books.csv")
header = booklist[0]
all_books = booklist[1:]

Template = open(r'c:\path\to\cell.txt', 'r')
sTemplate = Template.read()


author1 = getattr(all_books, 'author1')  # trying to assign the author
value to the variable author1.


sAuth = re.sub('%author%', author1, sTemplate) # replace %author1% with
the value in author1 in the variable sTemplate)

++
The getattr causes an error that says "AttributeError: 'list' object
has no attribute 'author1'" so I think I am reading the operator
library page on the python site wrong somehow.

once this works,  I move onto the second problem, which is to write out
the string after the substitions to a new file called e.g. foc2.txt
(for the frogs of canada book).

I really am not sure how to go about this.  I was thinking of something
like concatenating three strings together

path = "r/'c:\path\to"
code = getattr(code, all_books)
extension = '.txt/''
filename = path + code + extension

f = open(filename, 'w')

however I have serious doubts that doing that will work.

I guess what I am needing, if it doesn't, is some sort of method of
substituting a variable's value into the following:

f = open(r'c:\path\to\%subs%.txt, 'w')


The third thing I am hoping to acheive is to append an extra field to
each book called 'filename' which will simply be a concatenation of the
'code' field + '.txt'.   Further thought suggests that I should create
the field first, even if it is empty, so that the books still all
conform to the Book class.  Then it simply becomes a question of how to
I change the value of a field in these lists.  I am sure that the
answer to that was something I found in the tute, or it will be in my
deitel python book somewhere...  The hard bit is part one (assigning
the value of a field to a variable)

Thanks for your help.

Googleboy




More information about the Python-list mailing list