「Python|输入输出」如何进行用户输入、文件输入和输出
本文主要介绍Python中如何让用户进行内容输入,如何从文件中读取数据作为输入以及如何将数据保存到文件中
#python#开发语言#交互
Table of Contents
本文主要介绍Python中如何让用户进行内容输入,如何从文件中读取数据作为输入以及如何将数据保存到文件中
输入输出是程序中常见的一种场景,比如
- 让用户输入账户名密码(由于一般会是图形界面不是shell界面,由其他方式处理)
- 用户输入参数(比如简单写一个计算器,让用户输入数值)
- 从文件中读取数据(比如cookie,账户名密码等等)
- 将程序处理的结果保存到文件中
如何让用户进行内容输入
- python中的用户输入只需要调用python全局内置的
input()方法(python就是如此友好) input(prompt='')可以接受一个参数,用来作为引导用户进行输入的提示语,比如:input("请输入你的名字:")- 用户按下回车之后,回车之前的内容将会作为
input()方法的返回值 input()的返回值是字符串类型,(即用户输入的内容会先被作为文本对待),需要我们处理成我们希望的类型。比如我们使用input("请输入你的年龄"),那么我们希望得到的是一个整数,我们可以用int()方法将输入内容转换为整型,写为:age: int = int(input("请输入你的年龄:"))- 在算法题/面试题中常见需要进行数据输入的处理。比如输入一组数
1, 2, 3, 4, 5,则需要使用numbers = list(map(int, input().split(',')))得到一个整数的列表[1, 2, 3, 4, 5] - 多个输入或者多行输入的情况,使用多次
input()即可。
如何从文件中获取数据到程序(内存)中
大型应用/ 生成级别的python程序,处理的数据来源主要都是文件、网络或者其他程序处理的结果。如果我们要将文件内容读取到程序(内存)中,则需要使用文件输入相关的方法,比如python全局内置的open()方法,open()的返回值代表一个文件,使用这个文件对象的内置方法即可进行文件数据读取,主要常用的读取方法有.read(),.readline(),.readlines(),三个方法的函数说明如下:
Help on built-in function read:
read(size=-1, /) method of _io.TextIOWrapper instance
Read at most n characters from stream.
Read from underlying buffer until we have n characters or we hit EOF.
If n is negative or omitted, read until EOF.
Help on built-in function readline:
readline(size=-1, /) method of _io.TextIOWrapper instance
Read until newline or EOF.
Returns an empty string if EOF is hit immediately.
Help on built-in function readlines:
readlines(hint=-1, /) method of _io.TextIOWrapper instance
Return a list of lines from the stream.
hint can be specified to control the number of lines read: no more
lines will be read if the total size (in bytes/characters) of all
lines so far exceeds hint.各个方法的使用示例如下:
"""
假设我们有文本文件: 讲稿.txt,内容为如下三行:
这是一篇讲稿
这是开场白
这是结束语
"""
"""以下代码将使用.read()方法一次性读取文本文件中的全部内容"""
with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp:
content = fp.read()
print(f"content的类型为: {type(content)}")
print(f"content的内容为: {content}")
"""输出结果:
content的类型为: <class 'str'>
content的内容为: 这是一篇讲稿
这是开场白
这是结束语
"""
"""
以下代码将使用.read()方法一次性读取文本文件中的第一行内容
(多次调用将依次读取各行内容)
"""
with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp:
content = fp.readline()
print(f"content的类型为: {type(content)}")
print(f"content的内容为: {content}")
"""输出结果:
content的类型为: <class 'str'>
content的内容为: 这是一篇讲稿
"""
"""
以下代码将使用.read()方法一次性读取文本文件中的各行内容作为列表返回
每一行的内容作为返回的列表中的单个元素
"""
with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp:
content = fp.readlines()
print(f"content的类型为: {type(content)}")
print(f"content的内容为: {content}")
"""输出结果:
content的类型为: <class 'list'>
content的内容为: ['这是一篇讲稿\n', '这是开场白\n', '这是结束语']
"""如何将程序数据存储到文件中
- 在读取文件数据的时候,我们调用
open()时传入的第二个参数是‘r’,表示"read(读取文件内容)" - 而当我们要将数据写入到文件中的时候,我们需要传入
'w',表示"write(内容写入文件)" - 使用
'w'时,会清空文件原有的内容,然后将我们指定的内容写入到文件中 - 如果要保留原有内容,然后在原有内容的末尾插入我们指定内容,则传入
'a',表示"append(追加内容到文件中)"
with open('D:/讲稿.txt', 'w', encoding='utf-8') as fp:
fp.write("重写文稿内容")
"""此时我们重新读取文件内容,将是我们写入的内容"""
with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp:
content = fp.read()
print(f"content的内容为: {content}")
# 输出结果: content的内容为: 重写文稿内容
"""将文件内容进行换行,然后再写入一行内容,并重新读取内容检验"""
with open('D:/讲稿.txt', 'a', encoding='utf-8') as fp:
fp.write("\n")
fp.write("第一行内容")
with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp:
content = fp.read()
print(f"content的内容为: {content}")
"""输出结果:
content的内容为: 重写文稿内容
第一行内容
"""以上就是python基本的输入输出内容,快用起来吧!
好书推荐:
好课推荐:
写文不易,如果对你有帮助的话,来一波点赞、收藏、关注吧~👇