deleting the first and the last character of a string

Padraig Brady Padraig at Linux.ie
Wed Sep 11 07:31:17 EDT 2002


Manuel Hendel wrote:
> I got a file with fields seperated by a "|". The problem is that there
> is also a "|" at the beginning and at the end of each line although
> there no fields in front or behind these "|". Now I want to delete
> these two "|". Is there anything which says delete the first or last
> character of a string? I checked the string module on the web, but I
> couldn't find anything.
> 
> I solved it like this, but I honestly don't like it that way.
> 
> #!/usr/bin/env python
> #
> import sys
> import string
> 
> inputfile = open(sys.argv[-1], "r")
> 
> for line in inputfile.readlines():
>         fields = string.split(line, "|")
>         lineN = string.join(fields[1:-1], "|")
> 
> Isn't there any better solution? ;-)
> 
> Manuel
> 
How about: line = line[1:-1]

Pádraig.




More information about the Python-list mailing list