[Tutor] Can't transform a list of tokens into a text

Muhammad Ali ali.jan at gmail.com
Thu Jul 16 05:34:08 CEST 2009


Try changing all instances of list[i + 1] to list[i]

On Thu, Jul 16, 2009 at 8:33 AM, Eduardo Vieira <eduardo.susan at gmail.com>wrote:

> #===
> I tried this version to post in this forum but that gives me an error.
> I don't know why I don't get an error with the code above which is
> essentially the same:
> # -*- coding: cp1252 -*-
>
> result = ''
> lista = [
> 'No', ',', 'thanks.+ at +',
> 'He', 'said', ',', '"', 'no', ',', 'thanks', '.', '"', 'OK', '?', 'Hi']
>
> punct = '!#$%)*,-.:;<=>?@/\\]^_`}~”…'
> for i, item in enumerate(lista):
>
>    if item == '"' and lista[i + 1] not in punct:
>        result +=item
>        spacer = True
>    elif '+ at +' in item:
>        donewline = item.replace('+ at +','\n ')
>        result += donewline
>    elif item not in punct and lista[i + 1] in punct:
>        result += item
>    elif item in punct and lista[i + 1] in punct:
>        result += item
>    elif item in punct and lista[i + 1] == '"' and spacer:
>        result += item
>        spacer = False
>    elif item not in punct and lista[i + 1] == '"' and spacer:
>        result += item
>        spacer = False
>    elif item in '([{“':
>        result += item
>    else:
>        result += (item + " ")
>
> print result
>
> #==
> The error is this:
> Traceback (most recent call last):
>  File "<string>", line 244, in run_nodebug
>  File "C:\mytools\jointags-v4.py", line 17, in <module>
>    elif item not in punct and lista[i + 1] in punct:
> IndexError: list index out of range
>
> I'm using python 2.6.2 with PyScripter IDE
> I have tried a so many variations that I'm not sure what I'm doing any
> more....
> I'm just trying to avoid some post-processing with sed again.
>
> Thankful,
>
> Eduardo
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
http://alitechtips.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090716/2c9ab491/attachment.htm>


More information about the Tutor mailing list