Python输入读取格式

Python输入读取格式,python,input,format,Python,Input,Format,如何将python在一行中键入的两个值读入两个变量 如c中的%d%d 原始输入读取该行中的所有内容 例10 我想要x中的1和y中的0,您可以读取整行内容并使用正则表达式对其进行解析。使用组获取您感兴趣的部分。否则,如果不需要这样的控件,只需使用string.split。使用larsmans方法就足够了。如果你真的想要扫描,最重要的是,不要读整行。试试这个,它可能会很慢 x, y = map(int, raw_input().split()) import re import sys clas

如何将python在一行中键入的两个值读入两个变量 如c中的%d%d 原始输入读取该行中的所有内容

例10


我想要x中的1和y中的0,您可以读取整行内容并使用正则表达式对其进行解析。使用组获取您感兴趣的部分。否则,如果不需要这样的控件,只需使用string.split。

使用larsmans方法就足够了。如果你真的想要扫描,最重要的是,不要读整行。试试这个,它可能会很慢

x, y = map(int, raw_input().split())
import re
import sys

class Pin:
    formatDict = {'%d': r'(\d+)', '%f': r'(\d+\.?\d*)'}
    def __init__(self, input=sys.stdin):
        self.input = input

    def scanf(self, format):
        # change the C style format to python regex
        for a, b in self.formatDict.iteritems():
            format = format.replace(a, b)
        patt = re.compile('^\\s*%s$'%format, re.M)
        buf = ''
        matched = 0
        while 1:
            c = self.input.read(1)
            if not c: break
            buf += c
            g = patt.match(buf)
            if g:
                # matched, but there may be more to match, so don't break now
                matched = 1
                matchedGroup = g
            elif matched:
                # the first unmatch after a match, seek back one char and break now
                self.input.seek(-1, 1)
                break
        if matched:
            return tuple(eval(x) for x in matchedGroup.groups())

scanf = Pin(open('in', 'r')).scanf
print scanf('%d %d')
print scanf('%f %f')
丑陋但有趣,对吗