Strip last period from a host string

python, regex, replace

Solution

You do it like this:

hostname.rstrip('.')

where hostname is the string containing the domain name.

>>> 'domain.com'.rstrip('.')
'domain.com'
>>> 'domain.com.'.rstrip('.')
'domain.com'

Problem

I am having a hard time figuring out how to strip the last period from a hostname ... current output: - domain.com. - suddomain.com. - domain.com. - subdomain.subdomain.com. - subdomain.com. desired output: - domain.com - subdomain.com - domain.com - subdomain.subdomain.com attempt 1: ``` print string[:-1] #it works on some lines but not all ``` attempt 2: ``` str = string.split('.') subd = '.'.join(str[0:-1]) print subd # does not work at all ``` code: ``` global DOMAINS if len(DOMAINS) >= 1: for domain in DOMAINS: cmd = "dig @adonis.dc1.domain.com axfr %s |grep NS |awk '{print $1}'|sort -u |grep -v '^%s.$'" % (domain,domain) p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) string = p.stdout.read() string = string.strip().replace(' ','') if string: print string ```

Original source