zoukankan      html  css  js  c++  java
  • python_文本文件里面随机抽取若干行,写入新的文本文件里面

    天一直阴沉沉的,真想早点下班啊。。。。。。

     1 #encoding=utf-8
     2 import random
     3 from sets import Set
     4    
     5 def copyfile(srcfile, dstfile, linenum):
     6      """
     7          get linenum different lines out from srcfile at random
     8          and write them into dstfile
     9      """
    10      result = []
    11      ret = False
    12      try:
    13          srcfd = open(srcfile,'r')
    14      except IOError:
    15          print 'srcfile doesnot exist!'
    16          return ret
    17      try:
    18          dstfd = open(dstfile,'w')
    19      except IOError:
    20          print 'dstfile doesnot exist!'
    21          return ret
    22      srclines = srcfd.readlines()
    23      srclen = len(srclines)
    24      while len(Set(result)) < int(linenum):
    25          s = random.randint(0,srclen-1)
    26          result.append(srclines[s])
    27      for content in Set(result):
    28          dstfd.write(content)
    29      srcfd.close()
    30      dstfd.close()
    31      ret = True
    32      return ret
    33   
    34 if __name__ == "__main__":
    35      srcpath = raw_input('input srcfile path')
    36      dstpath = raw_input('input dstfile path')
    37      linenum = raw_input('input linenum')
    38      print copyfile(srcpath,dstpath,linenum)

     hadoop快点跑吧。。。。。。

  • 相关阅读:
    supervisord 小记
    linux 查找文件与进程常用命令
    旷世奇坑!!!spring 不能自动注入
    RPM方式安装MySQL5.6
    linux 常见问题&解决方案
    linux下的守护进程
    java 读写properties
    良好的编码规范
    良好的日志记录规范
    两种方式实现适配器
  • 原文地址:https://www.cnblogs.com/xupeizhi/p/2608577.html
Copyright © 2011-2022 走看看