python常用输入方式
逐字符读入
逐行读入
stdin
import sys
for line in sys.stdin:
# 去掉末尾换行符
line = line.strip()
print(line)
参考
https://blog.csdn.net/qq_41961459/article/details/104201928
https://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin
文件
把上面的sys.stdin
换成open('path/to/file')
:
for line in open('/path/to/file'):
# 去掉末尾换行符
line = line.strip()
print(line)
也可以f.readline()
读取单行。
参考:https://www.cnblogs.com/sysuoyj/archive/2012/03/14/2395789.html
一次性读取并按行保存为list
不保留每行末尾的换行符:
f.read().splitlines()
参考:https://stackoverflow.com/questions/3925614/how-do-you-read-a-file-into-a-list-in-python
保留每行末尾的换行符:
f.readlines()
来源:https://docs.python.org/3/tutorial/inputoutput.html
读取为整型:
[int(line) for line in open('hits_cdf')]
参考:https://stackoverflow.com/questions/6583573/how-to-read-numbers-from-file-in-python
读取参数
sys.argv
: 参数列表。sys.argv[0]
一般是当前命令。
len(sys.argv)
是参数个数。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!