[Tutor] Best way to truncate strings
Sean 'Shaleh' Perry
shalehperry@attbi.com
Sun Nov 3 18:59:01 2002
On Sunday 03 November 2002 14:41, Troy Sorzano wrote:
> Hi all,
>
> I have been messing around with python creating a script to read my
> firewall logs. The source and destination IP's in the log files
> include the port. For example 192.168.10.1:8080
> I want to remove the :8080 so I am left with only the IP address. The
> port is not always the same length. Here is and example of the code I
> wrote to remove the : and anything after it. Is there a better way to
> do this?
>
> Import string
> endpoints =3D []
> endpoints.append('192.168.1.10:8080')
> print endpoints[0][:string.find(endpoints[0],':')]
>
>>> s =3D '192.168.1.1:8080'
>>> s.split(':')
['192.168.1.1', '8080']
and if there is no port for some reason
>>> s =3D '192.168.1.1'
>>> s.split(':')
['192.168.1.1']
it still works (-: