如何只打印python中用连字符分隔的文件行中的数字?

时间:2017-11-12 19:53:50

标签: python regex string file module

输入文件中包含的文本如下 -

  

ORB Dr. Nick" B" argain医疗服务1-800-DOCT
原件   着名的Ray's Pizza 555-PIZA
Otto"我的驾驶" 555-8821
  犁王555-4796
Pretzel Wagon 555-3226由John Frink教授   实验室555-5782
收音机Psychaiatrist 555-7246

以下是预期输出 -

  

1 1800
  555个
  555 8821
555 4796
555 3226
555 5782
555 7246

我尝试使用re但无法得到我想要的确切内容。

2 个答案:

答案 0 :(得分:0)

试试这个(我为你写了一个函数):

{Policy=456, success_action_status=201, Signature=789, AWSAccessKeyId=123, acl=public-read, key=asdasd, Content-Type=text/plain}
AWSAccessKeyId=123
Policy=456
Signature=789
key=asdasd
Content-Type=text/plain
acl=public-read
success_action_status=201

答案 1 :(得分:-1)

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

您可以通过更改您希望机器查找的值来修复您想要的任何内容,但这样即使连字符也只能获取数字数据。 此外,我并不是这个问题的粉丝,因为它只询问文件中的数字,但随后您发布了与之相矛盾的细节。我只回答这个问题。 这不是最有效的代码,但它是可编辑的。 for循环中的14限制了机器打印的数据量,因为它看起来像你想要的那样。您可以根据需要更改限制。你真的不需要while循环它只是对测试有好处。如果你想在每个数字之间留一个空格,那么只需为每个结尾插入一个空格=""然后它会告诉你,如果你想要没有限制,你想要打印所有数据,那么只需删除:14。 当您输入所有数字

时,输出应为此