Python 官方文档:入门教程 => 点击学习
一,普通读入数据 有一下5种方式: n, m = [int(i) for i in temp.split(' ')] n, m = map(int,raw_input().split(' ')) import sys for lin
有一下5种方式:
n, m = [int(i) for i in temp.split(' ')]
n, m = map(int,raw_input().split(' '))
import sys
for line in sys.stdin:
for data in line.split(' '):
print data
import sys
arr = []
for line in sys.stdin:
arr.append([int(i) for i in line.split(' ')])
import sys
arr = []
for line in sys.stdin:
arr.append( set( line.lower.split(' ') ) )
while True:
try:
(x, y) = (int(x) for x in raw_input().split())
print x + y
except EOFError:
break
# windows下:
from ctypes import *
msvcrt = cdll.msvcrt
msg = "Hello world!\n"
msvcrt.printf("Testing: %s", msg)
# linux下:
from ctypes import *
libc = CDLL("libc.so.6")
msg = "Hello, world!\n"
libc.printf("Testing: %s", msg)
scanf()格式字符串 |
正则表达式 |
%c |
. |
\ |
.{5} |
%d |
[-+]?\d+ |
%e,%E,%f,%g |
[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)? |
%i |
[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+) |
%o |
[-+]?[0-7]+ |
%s |
\S+ |
%u |
\d+ |
%x,%X |
[-+]?(0[xX])?[\dA-Fa-f]+ |
print('scanf()')
pattern = re.compile(r"(\S+) - (\d+) errors, (\d+) warnings")
match = pattern.match('/usr/sbin/sendmail - 0 errors, 4 warnings')
if match:
print(match.groups())
scanf()
('/usr/sbin/sendmail', '0', '4')
print('scanf() %c')
pattern = re.compile(r".")
match = pattern.match('this is for test/n')
if match:
print(match.group())
结果输出如下:scanf() %c
t
print('scanf() \')
pattern = re.compile(r".{5}")
match = pattern.match('this is for test/n')
if match:
print(match.group())
结果输出如下:scanf() \
this
print('scanf() %e, %E, %f, %g')
pattern = re.compile(r"[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?")
match = pattern.match('+200.3721/n')
if match:
print(match.group())
match = pattern.match('x9876/n')
if match:
print(match.group())#不匹配没有输出
scanf() %e, %E, %f, %g
+200.3721
print('scanf() %i')
pattern = re.compile(r"[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)")
match = pattern.match('0xAA55/n')
if match:
print(match.group())
match = pattern.match('234.56/n')
if match:
print(match.group())
scanf() %i
0xAA55
234
print('scanf() %o')
pattern = re.compile(r"[-+]?[0-7]+")
match = pattern.match('0756/n')
if match:
print(match.group())
match = pattern.match('898/n')
if match:
print(match.group())#不匹配没有输出
结果输出如下:scanf() %o
0756
print('scanf() %s')
pattern = re.compile(r"\S+")
match = pattern.match('深圳是一个小渔村/n')
if match:
print(match.group())
match = pattern.match('898/n')
if match:
print(match.group())
结果输出如下:scanf() %s
深圳是一个小渔村
898
print('scanf() %u')
pattern = re.compile(r"\d+")
match = pattern.match('756/n')
if match:
print(match.group())
match = pattern.match('-898/n')
if match:
print(match.group())#不匹配没有输出
scanf() %u
756
print('scanf() %x %X')
pattern = re.compile(r"[-+]?(0[xX])[\dA-Fa-f]+")
match = pattern.match('0x756/n')
if match:
print(match.group())
match = pattern.match('-898/n')
if match:
print(match.group())#不匹配没有输出
scanf() %x %X
0x756
--结束END--
本文标题: python输入,格式化输入,以及sca
本文链接: https://www.lsjlt.com/news/186924.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0