python-文本操作和二进制储存
0x01 open方法
r read
w write
a append
b byte
test.txt内容为
yicunyiye
wutang
读取test.txt
f = open('test.txt','r',encoding='utf-8')
text = f.read()
print(text)
读取一行
f = open('test.txt','r',encoding='utf-8')
text = f.readline()
print(text)
全部
f = open('test.txt','r',encoding='utf-8')
text = f.readlines()
print(text)
输出
['yicunyiye\n', 'wutang\n']
美化
f = open('test.txt','r',encoding='utf-8')
text = f.readlines()
for t in text:
print(t.strip().replace("\n",""))
输出
yicunyiye
wutang
f = open('test.txt','r',encoding='utf-8')
for i in range(3):
text = f.readline()
print(text.strip().replace("\n",""))
输出同上
0x02 with open as
这种就不需要f.close()了
with open('test.txt','r',encoding='utf8') as f:
print(f.readlines())
写入文件
#w 不存在就创建 存在就覆盖
with open('test_two.txt','w',encoding='utf8') as f:
f.write('yicunyiye')
追加文件
#a 不存在就创建 存在就追加
with open('test_two.txt','a',encoding='utf8') as f:
f.write('\nniubi')
python-文本操作和二进制储存的更多相关文章
- python文件操作之二进制
列表项 三元运算符号: a=3 b=7 val=a if a>b else val=b print(val) 文件处理 首先给你一个文件,或者自己建立一个文件,那如何查看文件的内容呢? 1.安装 ...
- python自学笔记(五)python文本操作
一.python自带方法 r:read 读 w:write 写 a:append 尾行追加 先命令行进入python后 >>>d = open('a.txt','w') #在对应路径 ...
- python文本操作—读、写
文本文件存储的数据有很多,我们需要把这些文本里的内容读出来,然后在浏览器上面显示. 1.读取整个文本文件 格式: with open(路径) as 变量: 变量.read() 关键字with作用:在不 ...
- python文本操作
file_obj=file("文件路径","模式") 打开文件的模式有: r,以只读方式打开文件 w,打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该 ...
- Python文本操作2
# list3 = [# {"name": "alex", "hobby": "抽烟"},# {"name&q ...
- C++文本操作.Vs.Python
C++利用文件流: (1):读取一个字符 std::string TestTxt(argv[3]); // freopen(TestTxt.c_str(),"r",stdin);/ ...
- python 文本处理操作
打开和关闭文件 open 函数 用Python内置的open()函数打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写 ''' 模式 描述 r 以只读方式打开文件.文件的指针将会放在文 ...
- media静态文件统一管理 操作内存的流 - StringIO | BytesIO PIL:python图片操作库 前端解析二进制流图片(了解) Admin自动化数据管理界面
一.media ''' 1. 将用户上传的所有静态文件统一管理 -- settings.py -- MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 2. 服务 ...
- python中的文本操作
python如何进行文本操作 1.能调用方法的一定是对象,比如数值.字符串.列表.元组.字典,甚至文件也是对象,Python中一切皆为对象. str1 = 'hello' str2 = 'world' ...
随机推荐
- 兄弟,你爬虫基础这么好,需要研究js逆向了,一起吧(有完整JS代码)
这几天的确有空了,看更新多快,专门研究了一下几个网站登录中密码加密方法,比起滑块验证码来说都相对简单,适合新手js逆向入门,大家可以自己试一下,试不出来了再参考我的js代码.篇幅有限,完整的js代码在 ...
- 通过索引优化sql
sql语句的优化最重要的一点就是要合理使用索引,下面介绍一下使用索引的一些原则: 1.最左前缀匹配原则.mysql会一直向右匹配直到遇到范围查询(>.<.between.like)就停止匹 ...
- 华师2019软件专硕复试机试题最后一题G:找数
G. 找数 单点时限: 1.0 sec 内存限制: 256 MB 问题描述 输入一个整数 n( 2≤n≤10 ) ,你需要找到一些 n 位数(允许有前置 0 ,见样例),这些 n 位数均 由 0 ~ ...
- chromevue扩展 vue-devtools-master 谷歌vue的扩展程序
1,在百度网盘中下载压缩包,网盘地址:https://pan.baidu.com/s/1BnwWHANHNyJzG3Krpy7S8A ,密码:xm6s 2,将压缩包解压到F盘,F:\chromeVue ...
- Android开发值利用Intent进行put传值,setclass启动activity,并用get进行取值
传值方法一 [java] Intent intent = new Intent(); Bundle bundle = new Bundle(); //该类用作携带数据 bundle.putString ...
- Unable to add window -- token null is not for an application错误的解决方法 android开发
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not f ...
- python3笔记-字典
5 1 # 创建字典 6 2 d=dict(name='lily',age=18,phone='') 7 3 print(d) 4 # {'name': 'lily', 'age': 18, 'pho ...
- 再爆安全漏洞,这次轮到Jackson了,竟由阿里云上报
冤冤相报何时了,得饶人处且饶人.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习.关注公众号[ ...
- Linux命令使用教程
Linux 内核最初只是由芬兰人林纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux 的发行版说简单点就是将 Linux 内核与应用软件做一个打包. 目 ...
- 04async await
async async 函数返回值是一个promise对象,promise对象的状态由async函数的返回值决定 //函数的三种定义 async function hello() { return ...