-1
A
回答
0
使用larsmans的方法已經足夠了。如果你真的想要scanf的東西,而且最重要的是,不想看整行。儘管如此,它可能會很慢。
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')
醜但有趣,對不對?
5
x, y = map(int, raw_input().split())
0
您可以閱讀整行並使用正則表達式來解析它。使用組來獲取有趣的部分。否則,只要使用string.split,如果你不需要這樣的控制。
相關問題
- 1. 讀取matlab中的格式化輸入
- 2. Python的輸入格式
- 3. Python讀取輸出聲音不輸入
- 4. Python:讀取來自subprocess.communicate()的流輸入
- 5. Python機械化:讀取輸入旁邊的表格元素
- 6. python的JTIP文件格式讀取器
- 7. 如何讀取Java中的格式化輸入?
- 8. 如何讀取特定的輸入格式
- 9. basic python,讀取所有輸入的全局輸入?
- 10. Python中的格式化輸入
- 11. 閱讀彩色格式輸入打印python
- 12. Python從輸入文件讀取數據
- 13. 閱讀陣列格式輸入
- 14. 獲取格式不當的輸出(Python)
- 15. 數據讀取器格式化輸出
- 16. Python讀取非標準數字格式
- 17. 用戶輸入不是從HTML格式使用PHP讀取
- 18. 從python重定向標準輸入讀取輸入
- 19. 輸入格式
- 20. Python指定IP格式作爲輸入
- 21. 的Java讀取輸入
- 22. 自動輸入數據取決於我輸入的html格式
- 23. Python - 讀寫用戶輸入
- 24. python輸入被讀錯了?
- 25. 的Python - 格式化輸出
- 26. Python的格式輸出
- 27. 從「輸入」中讀取「從」輸入「
- 28. 獲取輸入特定格式在c
- 29. Javascript輸入格式
- 30. Matlab輸入格式