zoukankan      html  css  js  c++  java
  • Pyhton with语句练习 unix文件转换成dos文件

    win下面文件格式是

    >>> with open('/tmp/unix2dos.txt') as f:
    ...     data = f.read()
    ... 
    >>> data
    'MySQL 5.6 Reference Manual
    Including MySQL NDB Cluster 7.3-7.4 Reference Guide
    
    Abstract
    
    This is the MySQL? Reference Manual. It documents MySQL 5.6 through 5.6.37, as well as MySQL Cluster releases based on versions 7.3 and 7.4 of NDB through 5.6.36-ndb-7.3.18 and 5.6.36-ndb-7.4.16, respectively.
    
    MySQL 5.6 features.  This manual describes features that are not included in every edition of MySQL 5.6; such features may not be included in the edition of MySQL 5.6 licensed to you. If you have any questions about the features included in your edition of MySQL 5.6, refer to your MySQL 5.6 license agreement or contact your Oracle sales representative.
    
    For notes detailing the changes in each release, see the MySQL 5.6 Release Notes.
    
    For legal information, see the Legal Notices.
    
    For help with using MySQL, please visit either the MySQL Forums or MySQL Mailing Lists, where you can discuss your issues with other MySQL users. '

    linux文件格式是

    >>> with open('/etc/hosts') as f:
    ...     data = f.read()
    ... 
    >>> data
    '127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    '

    可以看到win文件是 结尾的linux文件是 结尾的

    import sys
    import os
    
    def unix2dos(fname):
        dfname = os.path.splitext(fname)[0] + '.win'
        with open(fname) as src_fobj:
            with open(dfname,'w') as dst_fobj:
                for line in src_fobj:
                    dst_fobj.write(line.rstrip('
    ')+ '
    ')
    
    if __name__ == '__main__':
        try:
            filename = sys.argv[1]
        except IndexError:
            print "usage:%s filename" % sys.argv[0]
        if not os.path.isfile(filename):
            print "No such file %s" % filename
            sys.exit(2)
        unix2dos(filename)
    
    [root@localhost untitled10]# python unix2dos.py  /etc/hosts
    
    >>> with open('/etc/hosts.win') as f:
    ...     data = f.read()
    ... 
    >>> data
    '127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
    '
  • 相关阅读:
    k近邻 KNN
    聚类之k-means
    支持向量机SVM、优化问题、核函数
    [THUSC 2016] 补退选 (Trie树)
    [CQOI2016] 手机号码 (数位dp)
    [CQOI2012] 交换棋子 (费用流)
    [SCOI2016] 背单词 (Trie树)
    [JSOI2009] 球队收益 (费用流)
    [BZOJ1878][SDOI2009] HH的项链 (树状数组)
    [BZOJ2151]种树
  • 原文地址:https://www.cnblogs.com/weiwenbo/p/6638513.html
Copyright © 2011-2022 走看看