How to read line by line a particular length of a file and write it to a list using python programme
Question: > This is my python programme
    
    
    IPS=[]                                      # creating a list of IP addresses
    w=sum(1 for line in open('my_dict.json'))   # w=total no. of lines in my_dict.json file 
    ins=open("my_dict.json","r")
    for lin in ins:          # from each line of my_dict.json,storing IP addresses in IPS   
        IPS.append(lin[2:10])
    print IPS
    

> Inside my_dict.json file IP addresses are stored as ["10.0.0.1/8"]
> ["10.0.0.2/8"]....["10.0.0.10/8"] ["10.0.0.11/8"]....etc..Now the problem
> is, for the IP values from 10.0.0.1 to 10.0.0.9 we can store that exactly in
> IPS as 10.0.0.1 to 10.0.0.9.But after that while 10.0.0.10,10.0.0.11 etc.
> comes then only 10.0.0.1 writes to IPS removing the last digit.Because we
> are writing lin[2:10] only.If we use it as lin[2:11] then the '/' value that
> immediately follows after ip address within the range 10.0.0.1 to 10.0.0.9
> will get written to IPS.So how should I modify programme to receive letters
> from 2 to 10 i.e lin[2:10] for ip addresses in the range 10.0.0.1 to
> 10.0.0.9 and after that letters from 2 to 11 i.e lin[2:11] for ip addresses
> 10.0.0.10,10.0.0.11,10.0.0.12 etc..


Answer: I think you can use regular expression to extract the IP part, code like this

    
    
    import re
    p = re.compile('\d+?\.\d+?\.\d+?\.\d+?')
    IPS=[]                                      # creating a list of IP addresses
    w=sum(1 for line in open('my_dict.json'))   # w=total no. of lines in my_dict.json file 
    ins=open("my_dict.json","r")
    for lin in ins:          # from each line of my_dict.json,storing IP addresses in IPS  
        result = p.search(a)
        if result is not None
            IPS.append(result.group(0))
    print IPS
    

