0

I have a text file that has a variable length of digits ('62554' could be 'xxxx' or 'xxx')

# cat dsset-domain.com.
domain.com.          IN DS 62554 8 1 84988C2B6D3C92F35E1457D3771D0270417622D2
domain.com.          IN DS 62554 8 2 EF940E2D5CFAD8B47A699ACCD68A77C43F120774CABCCA5A25443BE3 1D1BEABF

I need the last 4 strings of the first line assigned to list variables:

list[0] = 62554
list[1] = 8
list[2] = 1
list[3] = 84988C2B6D3C92F35E1457D3771D0270417622D2

But I don't exactly have a delimiter to work with.

What is the best way to slice up a line of strings of variable length and assign them as list elements?

code:

#!/usr/bin/python
import string
import requests

name = input("Enter the domain to provision DS keys for? ")
url = 'https://www.provider.net/ws/domain/'+name+'/ds'


with open('/usr/local/etc/namedb/dsset-'+name+'.', 'rt') as in_file:
        dstext = in_file.read()
print(dstext)

2 Answers 2

1

You can just use split ant pickup the last 3 item with slicing :

>>> s="domain.com.          IN DS 62554 8 1 84988C2B6D3C92F35E1457D3771D0270417622D2"
>>> s.split()[-3:]
['8', '1', '84988C2B6D3C92F35E1457D3771D0270417622D2']
Sign up to request clarification or add additional context in comments.

6 Comments

it says I have to wait 9 min to click the button. Thank you!
but he wants to get the values only from the first line.
@AvinashRaj So OP can apply this just on first line! ;)
@Kasra specifically -> dslist = dstext.split()[3:7] got it. But your concept worked and no loop required.
@mine yeah! surely! actually always there are more that 1 way to figure out the programming problems, but the winner is the faster!
|
0

fileobject.readline function will fetch only the first line.

with open('file') as f:
    l = []
    m = f.readline().split()
    l = m[-4:]
    print(l)

Output:

['62554', '8', '1', '84988C2B6D3C92F35E1457D3771D0270417622D2']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.