python对文档中元素删除,替换操作
作者:心之所向521 时间:2023-08-30 11:28:20
pthon更换文档中某元素:
with open('E:\\桌面\\train.txt', 'r') as f:
lines = f.readlines()
with open('E:\\桌面\\1704.txt', 'a') as ff: # 此处 w 和 a,均可,反正是新建的临时文件,文件不存在时会自动创建
for line in lines:
# kv = line.strip().split(' ')
kv = line.split(' ')
# kv[2] = kv[2]*100
m = float(kv[2])
m = round(m * 100, 1)
if kv[2]:
line = line.replace(kv[2], str(m))
ff.write(line)
# shutil.move('123.yml', 'apktool.yml')
python改变或者删除txt文档中某一列元素
##改变或者删除txt文档中某一列
import re
with open('E:\\桌面\\train.txt', 'r') as f: ##最开始要改变的文档
lines = f.readlines()
# for line in lines:
# line = re.split(' | ', line)
# print(line)
l = []
for line in lines:
# kv = line.strip().split(' ')
kv = line.strip(" ").split(' ')
kv.pop(3)
kv.pop(3)
# print(kv)
l.append(kv)
# print(l)
with open('E:\\桌面\\1706.txt', 'a') as ff: # 此处 w 和 a,均可,反正是新建的临时文件,文件不存在时会自动创建
for i in l:
print(i)
for i1 in i:
ff.write(i1) # 写入
ff.write(' ') # 有时放在循环里面需要自动转行,不然会覆盖上一条数据
ff.close()
with open('E:\\桌面\\1706.txt', 'r') as fff: ##读取临时文档
lines1 = fff.readlines()
with open('E:\\桌面\\1707.txt', 'a') as ffff: ###创建最后的要求的文档
for line in lines1:
i2 = line.strip(" ").split(' ')
n = float(i2[2])
m = round(n * 100, 1)
line = line.replace(i2[2], str(m))
ffff.write(line)
#
# line = i.replace(i[2], str(m))
# print(line)
# ff.write(line)
# for line in lines:
# # kv = line.strip().split(' ')
# kv = line.split(' ')
# kv.pop(3)
# kv.pop(4)
# # print(kv)
# l.append(kv)
# print(l)
#
#
# # m = float(kv[2])
# # m = round(m * 100, 1)
# # print(kv)
# # line = line.replace(kv[2], str(m))
# # print(line)
# # # line = line.replace(kv[3], '', 1)
# # # line = line.replace(kv[4], '', 1)
# # ff.write(line)
#
#
来源:https://blog.csdn.net/weixin_45564943/article/details/123380367
标签:python,文档,元素,删除,替换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mysql Binlog数据查看的方法详解
2024-01-27 18:26:22
获取当前url
2024-05-09 09:04:30
MySQL查询缓存的小知识
2024-01-22 20:55:29
![](https://img.aspxhome.com/file/2023/9/99339_0s.jpg)
解决Python 使用h5py加载文件,看不到keys()的问题
2021-10-04 06:19:15
asp如何编写sql语句来查询|搜索数据记录
2008-10-09 12:35:00
如何利用python执行txt文件中的代码
2021-03-01 04:47:46
用python实现的去除win下文本文件头部BOM的代码
2021-04-01 08:00:19
如何用GAN训练自己的数据生成新的图片
2023-04-13 16:45:07
![](https://img.aspxhome.com/file/2023/8/68398_0s.jpg)
浅谈四种快速易用的Python数据可视化方法
2023-12-14 08:08:30
![](https://img.aspxhome.com/file/2023/0/128580_0s.jpg)
用XsltArgumentList实现xsl的参数调用
2008-09-04 11:24:00
交互设计:简单
2011-08-27 16:46:27
python热力图实现简单方法
2023-10-28 06:14:41
使用Python操作Elasticsearch数据索引的教程
2021-03-04 22:51:28
![](https://img.aspxhome.com/file/2023/9/93019_0s.jpg)
解决MySQL安装重装时出现could not start the service mysql error:0问题的方法
2024-01-23 01:17:00
uniapp实现支付功能
2023-08-21 13:53:47
![](https://img.aspxhome.com/file/2023/4/55964_0s.png)
Python容器类型转换的3种方法实例
2022-06-03 13:32:32
![](https://img.aspxhome.com/file/2023/7/131207_0s.png)
django admin组件使用方法详解
2021-05-28 19:59:53
python实现将list拼接为一个字符串
2022-10-27 05:50:06
Python实战之疫苗研发情况可视化
2023-08-19 15:29:35
![](https://img.aspxhome.com/file/2023/4/102924_0s.png)
浅谈python可视化包Bokeh
2021-10-23 21:07:54