当前位置: 首页 > news >正文

搜索替换 csv 文件中的文本

【问题】

I just started with Python 3.4.2 and trying to find and replace text in csv file.

In Details,Input.csvfile contain below line:

0,0,0,13,.\New\_Path-1.1.12\Impl\Appli\Library\Module_RM\Code\src\Exception.cpp
0,0,0,98,.\Old_Path-1.1.12\Impl\Appli\Library\Prof_bus\Code\src\Wrapper.cpp
0,0,0,26,.\New_Path-1.1.12\Impl\Support\Custom\Vital\Code\src\Interface.cpp
0,0,0,114,.\Old_Path-1.1.12\Impl\Support\Custom\Cust\Code\src\Config.cpp

I maintained my strings to be searched in other file namedlist.csv

Module_RM
Prof_bus
Vital
Cust

Now I need to go through each line ofInput.csvand replace the last column with the matched string.

So my end result should be like this:

0,0,0,13,Module_RM
0,0,0,98,Prof_bus
0,0,0,26,Vital
0,0,0,114,Cust

I read the input files first line as a list. So text which i need to replace came inline[4]. I am reading each module name in thelist.csvfile and checking if there is any match of text inline[4]. I am not able to make thatifconditiontrue. Please let me know if it is not a proper search.

import csv
import re
with open("D:\\My_Python\\New_Python_Test\\Input.csv") as source, open("D:\\My\_Python\\\New_Python_Test\\List.csv") as module_names, open("D:\\My_Python\\New_Python_Test\\Final_File.csv","w",newline="") as result:
reader=csv.reader(source)
module=csv.reader(module_names)
writer=csv.writer(result)
#lines=source.readlines()
for line in reader:
for mod in module_names:
 if any(\[mod in s for s in line\]):
 line.replace(reader\[4\],mod)
 print ("YES")
 writer.writerow("OUT")
 print (mod)
module_names.seek(0)
lines=reader

Please guide me to complete this task.

Thanks for your support!

楼主自己解答了:

import csv
with open("D:\\My_Python\\New_Python_Test\\Input.csv") as source, open("D:\\My_Python\\New_Python_Test\\List.csv") as module_names, open("D:\\My_Python\\New_Python_Test\\Final_File.csv","w",newline="") as result:
 reader=csv.reader(source)
 module=csv.reader(module_names)
 writer=csv.writer(result)
 flag=False
 for row in reader:
 i=row[4]
 for s in module_names:
 k=s.strip()
 if i.find(k)!=-1 and flag==False:
 row[4]=k
 writer.writerow(row)
 flag=True
 module_names.seek(0)
 flag=False

【回答】

文本的结构化计算用集算器做会更独立些,不用写循环,SPL 脚本如下:

A
1=file("D:\\input.csv").import@c()
2=file("D:\\list.csv").import()
3=A1.run(_5=A2.select@1(pos(A1.#5,#1)).(#1))
4=file("D:\\result.csv").export(A1;",")

A3:从 A2 中查询满足条件的记录赋值给 A1 的 _5 字段

相关文章:

  • DBCO-PEG-Casein/Ovalbumin/Lectins点击化学DBCO偶联络蛋白/卵清蛋白/凝集素
  • 【JetPack Compose】声明式UI 、组合、重组
  • jquary样式操作、动画
  • Apollo配置语法——Apollo配置List和Map以及组合应用——非常详细
  • ESP8266-Arduino编程实例-MAX30102脉搏和心率监测传感器驱动
  • Connor学Android - Bitmap的加载和缓存策略
  • 最新Unity如何打包发布到Android
  • 34. 箭头函数与普通函数的区别?
  • IDC机房盘活资源省钱实例
  • Apache Shiro入门
  • 攻防世界WEB练习-inget
  • Apache Shiro简单介绍
  • 易语言生成短链接代码
  • Android滑动图片验证码
  • java类加载过程,双亲委派机制
  • [译]CSS 居中(Center)方法大合集
  • cookie和session
  • ECMAScript 6 学习之路 ( 四 ) String 字符串扩展
  • java中具有继承关系的类及其对象初始化顺序
  • js 实现textarea输入字数提示
  • JS题目及答案整理
  • PaddlePaddle-GitHub的正确打开姿势
  • Python实现BT种子转化为磁力链接【实战】
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • TCP拥塞控制
  • vue-router的history模式发布配置
  • Work@Alibaba 阿里巴巴的企业应用构建之路
  • 阿里云购买磁盘后挂载
  • 动态魔术使用DBMS_SQL
  • 猴子数据域名防封接口降低小说被封的风险
  • 将回调地狱按在地上摩擦的Promise
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 深入浅出webpack学习(1)--核心概念
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • [Shell 脚本] 备份网站文件至OSS服务(纯shell脚本无sdk) ...
  • hi-nginx-1.3.4编译安装
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • ​一些不规范的GTID使用场景
  • #NOIP 2014#Day.2 T3 解方程
  • (1)(1.11) SiK Radio v2(一)
  • (70min)字节暑假实习二面(已挂)
  • (PyTorch)TCN和RNN/LSTM/GRU结合实现时间序列预测
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (二)PySpark3:SparkSQL编程
  • (四)库存超卖案例实战——优化redis分布式锁
  • (转) Face-Resources
  • (转)ObjectiveC 深浅拷贝学习
  • (转)利用ant在Mac 下自动化打包签名Android程序
  • (转载)Linux 多线程条件变量同步
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • ./configure,make,make install的作用
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .FileZilla的使用和主动模式被动模式介绍
  • .NET Core 中的路径问题