因为正在参加PhysioNet Challenge 2019的比赛,其中数据根据题目的要求需要预处理一下,所以用python中的CSV库对约20万条数据进行了有规则的修改。修改的规则是:找到一行末尾字符是“1.0”,并且上一行的末尾字符是“0.0”的数据,并将该行以上12行内的数据末尾字符都改为“1.0”.

具体的实现过程是这样的:①将所有要修改的行找出来,并将该行的特征(行数,字符标记)存储到字典中;②对字典按照做相应的处理; ③新建一个文件,将源文件的每一行遍历,如果该行的特征不在字典中,则完全复制到新建文件,如果该行恰好在字典中,则将该行的最后一个字符改为“1.0”,在复制到新建文件中;④最后生成的新建文件就是我们需要的做出处理的数据文件。

代码和注释如下,亲测有效:

import csv
import numpy as np
"""
import csv and numpy.
"""
dictOrig = {}
"""
Define a dict to store the characteristics of the file need to be modified.
"""
with open('C:/Users/Ethan/PycharmProjects/readcsv/Sepsis.csv', 'r') as csvfile:
"""
Open the file need to be modified, note that the file path must be correct.
"""reader = csv.reader(csvfile)rows = [rowTemp for rowTemp in reader]"""rows = [['1.0','2.0','#','!'],['1.0','2.0','#','!'],['1.0','2.0','#','!'],[...],...]rows is a list collection of each row in the file."""for row in rows:if row[-1] == '1.0' and rows[rows.index(row)-1][-1] == '0.0':dictOrig[rows.index(row)] = row[-2]"""Travers the rows to record the characteristics of the file need to be modified in the dict.In this code, the feature we want to modify is that the end string of the row is '1.0' and the end string of the last row is '0.0'"""for temp in list(dictOrig.values()):"""Make some modifications to the feature, such as the code that we want to change the end string of the uo 12 rows fo the target line to '1.0'"""if float(temp) >= 13.0:"""If the index of target row is larger than 13, we should change the end of the previous 12row to '1.0'"""for i in range(12):dictOrig[list(dictOrig.keys())[list(dictOrig.values()).index(temp)]-1-i] = str(float(temp)-i-1)"""list(dictOrig.keys())[list(dictOrig.values()).index(temp)] is the key of the values temp"""if float(temp) < 13.0:"""If the index of target row is smaller than 13, we should change all the previous row to '1.0'"""for i in np.arange(0,float(temp),1.0):"""np.arange() is the range of float number"""dictOrig[list(dictOrig.keys())[list(dictOrig.values()).index(temp)]-1-i] = str(float(temp)-i-1)with open('C:/Users/Ethan/PycharmProjects/readcsv/New.csv', 'a', newline='') as newfile:"""Open a new file that we want to write to. Note that we must add the "newline=''" to avoid to generatethe blank lines. The parameter 'a' means that open for writing, appending to the end of the file if it exists."""for row in rows:"""Traverse the rows again."""if rows.index(row) in dictOrig.keys():"""If we find the row we want to change, change it at the time of replication."""rowNew = rowrowNew[-1] = '1.0'writer = csv.writer(newfile)writer.writerow(rowNew)else:"""Or we should replicate it directly."""rowNew = rowwriter = csv.writer(newfile)writer.writerow(rowNew)newfile.close()csvfile.close()"""Close the two files."""

Python小记--在特定位置修改CSV文件相关推荐

  1. python对数据进行分类、文件是csv文件_用Python将处理数据得到的csv文件分类(按顺序)保存...

    用Python中的os和numpy库对文件夹及处理数据后得到的文件进行分类保存: import numpy as np import os for m in range(699,0,-35): cur ...

  2. python导出数据找不到csv_【记录】使用Python读取/导出(写入)CSV文件

    想要用python处理csv文件. 去查了下,python中本身就自带csv模块. 然后参考在线手册: 去试试. [用python生成csv] 1. 按照手册的例子,试了试:import csv wi ...

  3. wps修改csv文件中的科学计数法修改为完整显示

    wps修改csv文件中的科学计数法修改为完整显示 问题 解决方案 问题 最近在工作中合作方提供了一个csv文件,直接用wps打开发现里面的id变为了科学计数法(如下图),也无法通过修改单元格格式在修改 ...

  4. Python读入制表位分割的csv文件

    Python读入制表位分割的csv文件 常见的csv文件都是以逗号进行分割的,但一次数据库导出的文件是以制表位间隔. 虽说另存为如下方式即可正常使用,但大批量的文件总归是不合适的 平时使用pandas ...

  5. Python之一个关于天气的csv文件中获取最高和最低气温的图表

    Python之一个关于天气的csv文件中获取最高和最低气温的图表 科普: csv: 简单的说:以逗号分隔的值(CSV)将数据写入文件中,这样的文件称为CSV文件 说明: 博主这里主要是学习一下csv文 ...

  6. python修改csv文件中列的数据类型_pandas读取CSV文件时查看修改各列的数据类型格式...

    下面给大家介绍下pandas读取CSV文件时查看修改各列的数据类型格式,具体内容如下所述: 我们在调bug的时候会经常查看.修改pandas列数据的数据类型,今天就总结一下: 1.查看: Numpy和 ...

  7. python修改csv文件字段顺序_python 操作csv文件写入顺序不对

    我是初学者不太懂 为什么在终端显示是正确的顺序到了csv 文件中就是另一回事了呢 还有就是 csv 文件怎么可以运行之后继续填写 而不是清空文件呢? 图片: 代碼: import urllib.req ...

  8. python pandas 把数据保存成csv文件,以及读取csv文件获取指定行、指定列数据

    文章目录: 1 数据说明 2 把数据集文件信息使用python pandas保存成csv文件 3 使用python pandas 读取csv的每行.每列数据 1 数据说明 1.在test_data目录 ...

  9. python通过DictReader实现两个csv文件的映射查找lookup之代码详解

    python里应用DictReader的实现匹配查找 需求简介 通过应用DictReader读取csv文件,实现通过一个CSV在另外个CSV里找到对应的解释,类似lookup. 数据内容 1 这里假定 ...

最新文章

  1. cisco路由器视频下载
  2. 扔掉 Postman,Apifox 才是 YYDS!
  3. Quartz框架中的JobStore
  4. 银行加息前存的定期会随着加息吗?
  5. [Linux]信号量
  6. 微信小程序运行的底层逻辑
  7. abp 上如何使用getstoredproccommand_多效唑,如何在柑橘上安全使用
  8. Property 'filter' does not exist on type 'Observable' 报错解决方法
  9. truffle version/usr/local/lib/node_modules/truffle/build/385.bundled.js:65232 catch cli.bundled.js:6
  10. 计算几何-求线段交点算法和代码(C++语言)
  11. Linux学习——linux网络配置、修改主机名、网络映射、远程连接工具XShell的使用(超详细)
  12. Hadoop高可用安装
  13. Python-----函数详解(上篇)(附小项目实战)
  14. android 在线预览pdf文件(目前最全)
  15. Android - 集成高德地图API(搜索,地图,定位)
  16. go 格式化占位符详解
  17. postgresql sha256
  18. 战高端,荣耀亮出“第二把剑”
  19. PHP学习路线、书单
  20. 大学生计算机课如何退出教学系统,如何把Windows 10的系统进程关闭?

热门文章

  1. Mysql 数据 导入 导出
  2. 2021年下半年网络工程师下午真题及答案解析
  3. Android之父深入解析Android
  4. mysql防止预约重号_mysql 防止重复插入唯一限制的数据
  5. 无法发送具有此谓词类型的内容正文_采用多模态细化类型进行程序合成
  6. python中怎么比较两个列表的大小_python中对列表元素大小排序(冒泡排序法,选择排序法和插入排序法)—排序算法...
  7. 大白话5分钟带你走进人工智能-第31节集成学习之最通俗理解GBDT原理和过程
  8. SSD磁盘测试不达标排查
  9. JS 浏览器扩展storage
  10. GetModuleHandle