python按索引删除字符串,基于特定的索引字符串值选择记录,然后通过python删除后续字段...
I have a .csv file named file01.csv that contains many records. Some records are required and some are not. I find that the required records has a string variable “Mi”, but it is not exist into the unnecessary records. So, I want to select the required records based on string value “Mi” in the field for every records.
Finally I want to delete the subsequent fields of each record from the field that contains value “Mi”. Any suggestion and advice is appreciated.
Optional:
In addition, I want to delete the first column.
Split column BB into two column named as a_id, and c_id. Separate the value by _ (underscore) and left side will go to a_id, and right side will go to c_id.
My fileO.csv is as follows:
AA BB CC DD EE FF GG
1 1_1.csv (=0 =10" 27" =57 "Mi"
0.97 0.9 0.8 NaN 0.9 od 0.2
2 1_3.csv (=0 =10" 27" "Mi" 0.5
0.97 0.5 0.8 NaN 0.9 od 0.4
3 1_6.csv (=0 =10" "Mi" =53 cnt
0.97 0.9 0.8 NaN 0.9 od 0.6
4 2_6.csv No Bi 000 000 000 000
5 2_8.csv No Bi 000 000 000 000
6 6_9.csv less 000 000 000 000
7 7_9.csv s(=0 =26" =46" "Mi" 121
My Expected results files (outFile.csv):
a_id b_id CC DD EE FF GG
1 1 0 10 27 57
1 3 0 10 27
1 6 0 10
7 9 0 26 46
解决方案
The following approach should work fine using Python csv module:
import csv
import re
import string
output_header = ['a_id', 'b_id', 'CC', 'DD', 'EE', 'FF', 'GG']
sanitise_table = string.maketrans("","")
nodigits_table = sanitise_table.translate(sanitise_table, string.digits)
def find_mi(row):
for index, col in enumerate(row):
if col.find('Mi') != -1:
return index
return -1
def sanitise_cell(cell):
return cell.translate(sanitise_table, nodigits_table) # Keep digits
f_input = open('fileO.csv', 'rb')
f_output = open('outFile.csv', 'wb')
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)
input_header = next(f_input)
csv_output.writerow(output_header)
for row in csv_input:
#print '%2d %s' % (len(row), row)
if len(row) >= 2:
bb = re.match(r'(\d+)__(\d+).0\.csv', row[1])
mi = find_mi(row)
if bb and mi != -1:
row[:] = row[:mi] + [''] * (len(row) - mi)
row[:] = [sanitise_cell(col) for col in row]
row[0] = bb.group(1)
row[1] = bb.group(2)
csv_output.writerow(row)
f_input.close()
f_output.close()
outFile.csv will contain the following:
a_id,b_id,CC,DD,EE,FF,GG
1,1,0,10,27,57,
1,3,0,10,27,,
1,6,0,10,,,
7,9,0,26,46,,
Tested using Python 2.6.6
python按索引删除字符串,基于特定的索引字符串值选择记录,然后通过python删除后续字段...相关推荐
- python中fillna_python中fillna_python – 基于特定列属性的Pandas fillna()
对我来说工作: df.ix[df['Type'] == 'Dog', 'Killed'] = df.ix[df['Type'] == 'Dog', 'Killed'].fillna(2.25) pri ...
- [Python Tips] 从list中选择”除某个特定元素之外“的值
Problem: 在编程过程中,需要加入list中除某个特定元素之外的值 Solution: 可以通过利用Python中set的差来排除特定元素 Example: 设有列表 A: A = [(-1,- ...
- mysql级联删除外键约束_MySQL外键约束、级联更新、级联删除
MySQL支持外键(foreign key),外键允许跨表交叉引用相关数据,另外外键约束能够保证相关数据的一致性.一个外键关系通常包括一个父表和一个子表.父表包含原始的字段数据,子表引用父表中该字段的 ...
- 实验七 索引和视图的操作Mysql索引和视图
实验七 索引和视图的操作 [实验目的]:①学会使用update.delete.insert命令 ②掌握视图的建立.修改.更新和删除. ③掌握索引的创建和使用. [实验内容]:相关命令写在作业本上. 1 ...
- python培训班靠谱吗-Python培训机构哪家好?如何选择靠谱的Python培训机构?
学python进入互联网行业,很多人会选择参加python培训的方式,市面上大小python培训机构层出不穷,教学水平和质量良莠不齐,但无一例外都会有各种好的条件吸引你做出选择,那如何在众多pytho ...
- 计算机的文件打开记录怎么删,怎样删除最近使用的文档记录,电脑文档文件怎么删除...
隐私,一直都是人们所注重的. 在电脑的使用上,很多人也很注重隐私. 自己的私人电脑,不允许其他人擅自使用. 同时对于隐私的保护,很多人在使用完电脑后悔清除相关的记录. 大家一般都会在电脑上使用文档,那 ...
- Oracle索引梳理系列(八)- 索引扫描类型及分析(高效索引必备知识)
理解oracle索引扫描类型的特点以及具体触发的条件,对于通过合理地使用索引,进行sql优化至关重要(例如组合索引的引导列的选择问题). 在总结索引扫描类型前,需要再次强调关于索引特点的几个关键点: ...
- python 如何删除excel中带有删除线的特定字符串保留没有删除线的单词
python 如何删除excel中带有删除线的特定字符串保留没有删除线的单词 注意:经验证 Mac必须安装Microsoft Excel app 但不一定OK import xlwings as xw ...
- python中字符串比较是基于字典序的_Python字符串字典集合
字符串 定义:字符串是一个有序的字符的集合,用于存储和表示基本的文本信息,'单引号' 或"双引号" 或 """三引号""" ...
最新文章
- Android多点触控MultiTouch浅析
- MaskRCNN路标:TensorFlow版本用于抠图
- ST-Link如何秒变J-link,手把手教你实现该功能
- Cannot forward after response has been committed问题解决及分析
- IIS虚拟目录控制类
- Spring教程笔记8 基于SHH的员工信息管理系统
- swagger 上传文件 参数_跟我一起学.NetCore之Swagger让前后端不再烦恼及界面自定义...
- P2P下载器-P2P种子搜索器
- POJ-1067 取石子游戏(威佐夫博弈)
- 京东返利PHP采集关键字,php-爬虫练习:抓取京东商品列表与详情-2019年10月18日...
- Ubuntu的踩坑记录
- 三门问题详解(附C语言实现)
- python中θ怎么打_python中%的用法
- EBS 修改系统名称
- 钢铁侠马斯克,当上了美国院士
- 超实用的公众号运营经验总结分享
- Vue 全套教程(一),入门 Vue 必知必会
- 计算机辅助绘图中测距在哪,我CAD怎么测量距离不行
- ArcGIS 10 破解方案
- 微信公众号开发时返回中文数据为问号解决方法