Table of Contents
如何让用户进行内容输入
- 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()
,三个方法的函数说明如下:
1Help on built-in function read:
2
3read(size=-1, /) method of _io.TextIOWrapper instance
4 Read at most n characters from stream.
5
6 Read from underlying buffer until we have n characters or we hit EOF.
7 If n is negative or omitted, read until EOF.
8
9
10Help on built-in function readline:
11
12readline(size=-1, /) method of _io.TextIOWrapper instance
13 Read until newline or EOF.
14
15 Returns an empty string if EOF is hit immediately.
16
17
18Help on built-in function readlines:
19
20readlines(hint=-1, /) method of _io.TextIOWrapper instance
21 Return a list of lines from the stream.
22
23 hint can be specified to control the number of lines read: no more
24 lines will be read if the total size (in bytes/characters) of all
25 lines so far exceeds hint.
各个方法的使用示例如下:
1""" 2假设我们有文本文件: 讲稿.txt,内容为如下三行: 3这是一篇讲稿 4这是开场白 5这是结束语 6""" 7 8"""以下代码将使用.read()方法一次性读取文本文件中的全部内容""" 9with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp: 10 content = fp.read() 11 print(f"content的类型为: {type(content)}") 12 print(f"content的内容为: {content}") 13"""输出结果: 14content的类型为: <class 'str'> 15content的内容为: 这是一篇讲稿 16这是开场白 17这是结束语 18""" 19 20""" 21以下代码将使用.read()方法一次性读取文本文件中的第一行内容 22(多次调用将依次读取各行内容) 23""" 24with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp: 25 content = fp.readline() 26 print(f"content的类型为: {type(content)}") 27 print(f"content的内容为: {content}") 28"""输出结果: 29content的类型为: <class 'str'> 30content的内容为: 这是一篇讲稿 31 32""" 33 34 35""" 36以下代码将使用.read()方法一次性读取文本文件中的各行内容作为列表返回 37每一行的内容作为返回的列表中的单个元素 38""" 39with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp: 40 content = fp.readlines() 41 print(f"content的类型为: {type(content)}") 42 print(f"content的内容为: {content}") 43"""输出结果: 44content的类型为: <class 'list'> 45content的内容为: ['这是一篇讲稿\n', '这是开场白\n', '这是结束语'] 46"""
如何将程序数据存储到文件中
- 在读取文件数据的时候,我们调用
open()
时传入的第二个参数是‘r’
,表示"read(读取文件内容)"
- 而当我们要将数据写入到文件中的时候,我们需要传入
'w'
,表示"write(内容写入文件)"
- 使用
'w'
时,会清空文件原有的内容,然后将我们指定的内容写入到文件中 - 如果要保留原有内容,然后在原有内容的末尾插入我们指定内容,则传入
'a'
,表示"append(追加内容到文件中)"
1with open('D:/讲稿.txt', 'w', encoding='utf-8') as fp: 2 fp.write("重写文稿内容") 3 4"""此时我们重新读取文件内容,将是我们写入的内容""" 5with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp: 6 content = fp.read() 7 print(f"content的内容为: {content}") 8 # 输出结果: content的内容为: 重写文稿内容 9 10 11"""将文件内容进行换行,然后再写入一行内容,并重新读取内容检验""" 12with open('D:/讲稿.txt', 'a', encoding='utf-8') as fp: 13 fp.write("\n") 14 fp.write("第一行内容") 15 16with open('D:/讲稿.txt', 'r', encoding='utf-8') as fp: 17 content = fp.read() 18 print(f"content的内容为: {content}") 19"""输出结果: 20content的内容为: 重写文稿内容 21第一行内容 22"""
以上就是python基本的输入输出内容,快用起来吧!
好书推荐:
好课推荐:
写文不易,如果对你有帮助的话,来一波点赞、收藏、关注吧~👇