Meng Xiangxi的个人博客分享 http://blog.sciencenet.cn/u/MengXiangxi

博文

处理数据的脚本备份

已有 2580 次阅读 2015-12-29 15:09 |系统分类:科研笔记

用法:把程序放在某个目录下,在相应目录下建一个"orginal"文件夹,把原始数据放进去;再建一个"processed"文件夹。

修改程序里面的判断条件什么的,然后运行就好了。

  • #!/usr/bin/env python
  • # -*- coding: utf-8 -*-
  •  
  • import os
  •  
  • # Transcribe is used to single out the interested rows
  • def transcribe(fname):
  •    finname = "Original/"+fname
  •    foutname = "Processed/Data_"+fname
  •    fin = open(finname,"r")
  •    fout = open(foutname,"w")
  •    line = fin.readline()
  •    # Find the first line of data
  •    while line.strip().split('t')[0] <> 'Number':
  •        line = fin.readline()
  •    while line:
  •        fout.write(line.strip().split('t')[0]+'t'+line.strip().split('t')[3]+"n"),
  •        # line.srip().split() is used to select the desired rows separated by tab
  •        line = fin.readline()
  •    fin.close
  •    fout.close
  •    print 'Transcription of %s completed.' %finname[9:]
  •  
  •  
  • # Traverse all the files in the folder
  • currentdir = os.getcwd()+'\Original'
  • directory =  os.listdir(currentdir)
  •  
  • for filename in directory:
  •    transcribe(filename)

process.py



https://blog.sciencenet.cn/blog-270448-946670.html

上一篇:资格考试回忆
下一篇:甲卡西酮的诞生——一点历史钩沉
收藏 IP: 124.205.76.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-25 13:34

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部