how to print only digits from file line separated by hyphen in python

ORB Dr Nick's "B"argain Medical Services 1-800-DOCT Original Famous Ray's Pizza 555-PIZA Otto's "How's my Driving" 555-8821 Plow King 555-4796 Pretzel Wagon 555-3226 Prof John Frink's Lab 555-5782 Radio Psychaiatrist 555-7246 This is the input file

Below is the output expected

1 1800 555 555 8821

I tried using re but couldnt get what exact i want.

2 answers

  • answered 2017-11-12 20:20 DRPK

    Try This ( i wrote a function for you ):

    def re_make_string(get_string):
    
        buffer_last = []
        for chars in get_string:
    
            if chars == "-" or chars == " ":
                buffer_last.append(" ")
    
            try:
                int(chars)
            except:
                pass
            else:
                buffer_last.append(chars)
    
        re_data = ''.join(buffer_last).replace("   ", " ").replace("  ", " ").strip()
        return re_data
    
    
    print(re_make_string("""ORB Dr Nick's "B"argain Medical Services 1-800-DOCT Original Famous Ray's Pizza 555-PIZA Otto's "How's my Driving" 555-8821 Plow King 555-4796 Pretzel Wagon 555-3226 Prof John Frink's Lab 555-5782 Radio Psychaiatrist 555-7246"""))
    

  • answered 2017-11-12 20:58 Ni Nisan Nijackle

    while True:
        data = input() # you could also switch input for the data you have above
        #put whatever you want to the script to look for below for a specific character
    
        digitOne = '1'
        digitTwo = '2'
        digitThree = '3'
        digitFour = '4'
        digitFive = '5'
        digitSix = '6'
        digitSeven = '7'
        digitEight = '8'
        digitNine = '9'
        for i in range(0, len(data[:14])):
            if data[i] == digitOne or data[i] == digitTwo or data[i] == digitThree:
                print(data[i], end = "")
            elif  data[i] == digitFour or data[i] == digitFive or data[i] == digitSix:
                print(data[i], end = "")
            elif  data[i] == digitSeven or data[i] == digitEight or data[i] == digitNine:
                 print(data[i], end = "")
            else:
                print("", end = "")
            #print('\n') not necesary ,but if you want to start the input on the new line
    

    You could fix whatever you want by changing the values you want the machine to look for ,but that's how you get the data of digits only even with hyphens. Also I am not really a fan of this question because it only asks for digits from a file ,but then you post the details that contradicts it. I am only answering the question. That isn't exactly the most efficient of code ,but it's editable. The :14 in the for loop limits the amount of data the machine prints ,since it seems like what you wanted is there. You can change the limit as you please. You really don't need the while loop it's just good for testing. if you want a space in between each number then simply insert a space for each end = "" then it will give you that if you want no limit and you want all the data to be printed then just remove the :14. The out put should be this when you type in the all the numbers