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
    '
  • 相关阅读:
    解决SecureCRT连接linux终端中文显示乱码
    处理二维数组两题
    struts2校验总结
    专题:分治法
    常用表格编写
    写Action的三种方法
    Struts2入门-十分简单的登陆
    汉诺塔动画
    记录一下字符串处理的问题
    记录一些指针的应用方法
  • 原文地址:https://www.cnblogs.com/weiwenbo/p/6638513.html
Copyright © 2011-2022 走看看