I am having a hard time figuring out how to strip the last period from a hostname ...
current output:
desired output:
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
The string strip() method in python is built-in from Python. It helps the developer to remove the whitespaces or specific characters from the string at the beginning and end of the string. Strip() method in string accepts only one parameter which is optional and has characters.
Use Python to Remove Punctuation from a String with Translate. One of the easiest ways to remove punctuation from a string in Python is to use the str. translate() method. The translate method typically takes a translation table, which we'll do using the .
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With