Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python解析用户输入_Python_Parsing_Input_Numbers_Range - Fatal编程技术网

用Python解析用户输入

用Python解析用户输入,python,parsing,input,numbers,range,Python,Parsing,Input,Numbers,Range,我需要解析来自用户的输入,使其具有以下格式之一: 1321 .. 123123 或 一个数字(可以是负数)、一个逗号、或两个点。,然后是另一个数字(可以是负数) 另外,第一个数字必须小于或等于正则表达式适用于我,请将它们直接应用于raw\u input()返回的值。例如: import re s1 = '1321 .. 123123' s2 = '-21323 , 1312321' s3 = '- 12312.. - 9' [int(x) for x in re.findall(r'[^,

我需要解析来自用户的输入,使其具有以下格式之一:

 1321 .. 123123

一个数字(可以是负数)、一个逗号
或两个点
,然后是另一个数字(可以是负数)


另外,第一个数字必须小于或等于
正则表达式适用于我,请将它们直接应用于
raw\u input()
返回的值。例如:

import re
s1 = '1321 .. 123123'
s2 = '-21323 , 1312321'
s3 = '- 12312.. - 9'

[int(x) for x in re.findall(r'[^,.]+', ''.join(s1.split()))]
=> [1321, 123123]

[int(x) for x in re.findall(r'[^,.]+', ''.join(s2.split()))]
=> [-21323, 1312321]

[int(x) for x in re.findall(r'[^,.]+', ''.join(s3.split()))]
=> [-12312, -9]
首先使用
进行拆分,然后使用


我想这会有帮助。

看看这个。大多数人会说使用正则表达式,但是如果数字总是整数,有更简单的方法。好的,如果
-
和数字分开,例如
-12312..-9
@chamini2我用处理那个案件的代码更新了我的答案
def ask_range():
    raw = raw_input()
    raw = raw.strip(' \t\n\r')
    raw = map((lambda x: x.split("..")), raw.split(","))
    raw = list(item for wrd in raw for item in wrd)
    if len(raw) != 2:
        print "\nexpecting a range value, try again."
        return ask_range()
def ask_range():
    raw = raw_input()
    raw = raw.strip(' \t\n\r')
    raw = re.split(r"\.\.|,", raw)
    if len(raw) != 2:
        print "\nexpecting a range value, try again."
        return ask_range()

    left = re.match(r'^\s*-?\s*\d+\s*$', raw[0])
    right = re.match(r'^\s*-?\s*\d+\s*$', raw[1])
    if not (left and right):
        print "\nexpecting a range value, try again."
        return ask_range()

    left, right = int(left.group()), int(right.group())
    if left > right:
        print "\nexpecting a range value, try again."
        return ask_range()

    return left, right
import re
s1 = '1321 .. 123123'
s2 = '-21323 , 1312321'
s3 = '- 12312.. - 9'

[int(x) for x in re.findall(r'[^,.]+', ''.join(s1.split()))]
=> [1321, 123123]

[int(x) for x in re.findall(r'[^,.]+', ''.join(s2.split()))]
=> [-21323, 1312321]

[int(x) for x in re.findall(r'[^,.]+', ''.join(s3.split()))]
=> [-12312, -9]
def ask_range():
        raw = raw_input()
        lis = []
        split1 = raw.split("..")
        for i in split1:
            try:
                lis.append(int(i))
            except:
                for j in i.split(","):
                    list.append(int(j))
        if len(raw) != 2:
            print "\nexpecting a range value, try again."
            return ask_range()
        return lis