python输入,格式化输入,以及scanf的替代方案

2024-05-06 22:48

本文主要是介绍python输入,格式化输入,以及scanf的替代方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一,普通读入数据

有一下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 dataimport 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 + yexcept EOFError:break


二,调用c标准库

# 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

在Python里,没有与scanf()直接等同的功能函数,因此需要格式化输入,就需要使用正则表达式的功能来实现,并且正则表达式的功能比scanf()更加灵活,功能更加强大,下面就来列出一些等同的表达:



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]+


输入一个字符串的例子:

/usr/sbin/sendmail - 0 errors, 4 warnings
对于上面格式的字符串,如果使用C函数scanf()来输入,需要使用下面的格式来实现:
%s - %d errors, %d warnings
如果我们使用正则表达式来表示,如下:
(/S+) - (/d+) errors, (/d+) warnings
例子:
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')

%c的例子:

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 

%e, %E, %f, %g的例子:

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

%i的例子:

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

八进制的%o的例子:

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

字符串%s的例子:

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

%u的例子:

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

十六进制%x, %X的例子:

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





这篇关于python输入,格式化输入,以及scanf的替代方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/965606

相关文章

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

MySQL精准控制Binlog日志数量的三种方案

《MySQL精准控制Binlog日志数量的三种方案》作为数据库管理员,你是否经常为服务器磁盘爆满而抓狂?Binlog就像数据库的“黑匣子”,默默记录着每一次数据变动,但若放任不管,几天内这些日志文件就... 目录 一招修改配置文件:永久生效的控制术1.定位my.cnf文件2.添加核心参数不重启热更新:高手应

python通过curl实现访问deepseek的API

《python通过curl实现访问deepseek的API》这篇文章主要为大家详细介绍了python如何通过curl实现访问deepseek的API,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编... API申请和充值下面是deepeek的API网站https://platform.deepsee

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

MySQL中like模糊查询的优化方案

《MySQL中like模糊查询的优化方案》在MySQL中,like模糊查询是一种常用的查询方式,但在某些情况下可能会导致性能问题,本文将介绍八种优化MySQL中like模糊查询的方法,需要的朋友可以参... 目录1. 避免以通配符开头的查询2. 使用全文索引(Full-text Index)3. 使用前缀索

Python将字库文件打包成可执行文件的常见方法

《Python将字库文件打包成可执行文件的常见方法》在Python打包时,如果你想将字库文件一起打包成一个可执行文件,有几种常见的方法,具体取决于你使用的打包工具,下面就跟随小编一起了解下具体的实现方... 目录使用 PyInstaller基本方法 - 使用 --add-data 参数使用 spec 文件(

Python MCPInspector调试思路详解

《PythonMCPInspector调试思路详解》:本文主要介绍PythonMCPInspector调试思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录python-MCPInspector调试1-核心知识点2-思路整理1-核心思路2-核心代码3-参考网址