[Tutor] Add newline's, wrap, a long string

Martin Walsh mwalsh at mwalsh.org
Wed Apr 29 15:37:39 CEST 2009


Sander Sweers wrote:
> 2009/4/29 David <david at abbottdavid.com>:
>> Here is the whole program so far, what it does is it logs into a druple web
>> site and posts. I would like to make it better, as you can see I do the same
>> thing over and over.
>>
>> http://linuxcrazy.pastebin.com/m7689c088
> 
> What you can do is define all the variables upfront. This way you can
> get rid of the else. Below is an example how you can do this with only
> looping once over the fle.
> 
> CBUILD = ''
> ACCEPT_KEYWORDS = ''
> 
> for line in fname:
>     if 'ACCEPT_KEYWORDS' in line:
>         output = line.split('"')[1]
>         ACCEPT_KEYWORDS = textwrap.fill(output, 80)
> 
>     if 'CBUILD' in line:
>         output = line.split('"')[1]
>         CBUILD = textwrap.fill(output, 80)


What is particularly important to note here is that Sander is iterating
over the file 'fname' only once. When you loop thru the lines of a file
with this form it is consumed, and you get nothing back for subsequent
attempts, unless you re-open the file, or explicitly move back to the
beginning, ie. fname.seek(0).


HTH,
Marty


> 
> etc etc
> 
> Greets
> Sander
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list