-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的標準輸入讀取行?
- 5. Python:讀取來自subprocess.communicate()的流輸入
- 6. 如何及時讀取輸入並格式化輸出?
- 7. Python機械化:讀取輸入旁邊的表格元素
- 8. python的JTIP文件格式讀取器
- 9. 用Python讀取FORTRAN格式的數字
- 10. 讀取在__DATA__格式的輸入文件中的Perl
- 11. 從XML格式的字符串讀取輸入並以TreeView格式顯示
- 12. 如何讀取Java中的格式化輸入?
- 13. 讀取C中的特定輸入格式
- 14. 如何讀取特定的輸入格式
- 15. 讀取文件針對特定的輸入格式
- 16. basic python,讀取所有輸入的全局輸入?
- 17. Python中的格式化輸入
- 18. Python:讀取和寫入複雜和重複格式的文件
- 19. 閱讀彩色格式輸入打印python
- 20. Python從輸入文件讀取數據
- 21. 獲取格式不當的輸出(Python)
- 22. Bash Zenity空格形式 - 閱讀輸入
- 23. 閱讀陣列格式輸入
- 24. 數據讀取器格式化輸出
- 25. 在python中讀取.xlsx格式
- 26. Python讀取非標準數字格式
- 27. 不能讀取這個python格式(,)[。 。 ]
- 28. 如何讀取MATLAB /倍頻格式化輸入?
- 29. 用戶輸入不是從HTML格式使用PHP讀取
- 30. 讀取輸入字符並以圖形格式打印出現