zoukankan      html  css  js  c++  java
  • python-03

    编写一个计时器,要求只能在一行显示,不能显示一个新的数字
    1. #!/usr/bin/env python
    2. import time
    3. import sys
    4. def timer(num = 60):
    5. for i in range(1, num+1):
    6. sys.stdout.write(' %d' % i)
    7. sys.stdout.flush()
    8. time.sleep(1)
    9. if __name__ == '__main__':
    10. timer()
    编写一个会动的字符
    1. #!/usr/bin/env python
    2. import time
    3. import sys
    4. i = 0
    5. sys.stdout.write('#' * 20)
    6. sys.stdout.flush()
    7. while True:
    8. sys.stdout.write(' %s@' % ('#' * i))
    9. sys.stdout.flush()
    10. i += 1
    11. try:
    12. time.sleep(0.4)
    13. except KeyboardInterrupt:
    14. print ' exit'
    15. break
    16. if i == 20:
    17. sys.stdout.write(' ' + '#' * 20)
    18. sys.stdout.flush()
    19. i = 0
    linux换行为 ,windows换行为
    使用unix2dos 从LINUX转换为windows文档
    使用dos2unix 从windows转换为linux文档  
    yum install unix2dos dos2unix -y
    unix2dos file1
    dos2unix file2
    在没有yum环境下可使用python编写一个小程序
    1. #!/usr/bin/env python
    2. import sys
    3. f = file(sys.argv[1])
    4. date = []
    5. while True:
    6. aline = f.readline()
    7. if len(aline) == 0:
    8. break
    9. newline = aline.rstrip() + ' '
    10. date.append(newline)
    11. f.close()
    12. fd = file('lw.txt', 'w')
    13. fd.writelines(date)
    14. fd.close()
    使用函数
    1. #!/usr/bin/env python
    2. #coding: utf8
    3. import sys
    4. def unix2dos(fname):
    5. fd = file(fname)
    6. dstfile = raw_input('please is file name: ')
    7. newf = file(dstfile, 'w')
    8. data = ['%s%s' % (eachline.rstrip(), ' ') for eachline in fd]
    9. newf.writelines(data)
    10. fd.close()
    11. newf.close()
    12. if __name__ == '__main__':
    13. unix2dos(sys.argv[1])

    一、列表拼接
    1. >>> alist = [1,2]
    2. >>> alist += [3]    //将两个列表拼接
    3. >>> alist
    4. [1, 2, 3]
    5. >>> alist += 3        //错误,不能将数字和列表拼接
    二、交换变量的值
    1. >>> x = 3
    2. >>> y = 'a'
    3. >>> x, y = y, x
    4. >>> x
    5. 'a'
    6. >>> y
    7. 3
    三、关键字
    python 保留了相关的一些单词,用于语法组成,被称作关键字,这些关键字用户不应该覆盖。
    1、
    >>>import random        //导入random模块,它的全部属性都可以使用,如在使用chioice的时候,写法是random.choice()

    2、
    >>>from random import choice        //仅将random中的choice导入。其他功能不可用,直接使用chioce()即可,不用加random

    3、
    >>>from random import *            //将random的全部属性导入,但是如果random中有以_开头的属性,就不能导入

    4、
    >>>imort random as rdm         //将random导入后,改名为rdm。使用它的choice语句是:ram.choice()       相当于一个改了名字

    1. #!/usr/bin/env python
    2. import os
    3. ls = os.linesep
    4. while True:
    5. fname = raw_input('file name: ')
    6. if os.path.exists(fname):
    7. print "ERRORL '%s' " % fname
    8. else:
    9. break
    10. all = []
    11. print " ( . ) to exit. "
    12. while True:
    13. entry = raw_input('>')
    14. if entry == '.':
    15. break
    16. all.append(entry)
    17. fobj = open(fname, 'w')
    18. fobj.writelines(['%s%s' % (x, ls) for x in all])
    19. fobj.close()
    20. print 'Done!'





  • 相关阅读:
    mysql 关联关系
    Powershell
    判断Server Manager里面的Role是否已经安排
    Powershell 获取文件版本信息
    PowerShell---Operators 介绍
    C#代码覆盖率 -vsinstr和OpenCover
    敏捷测试介绍
    c#中abstract、override、new、virtual、sealed使用
    装箱和拆箱
    Code Review
  • 原文地址:https://www.cnblogs.com/fina/p/6197315.html
Copyright © 2011-2022 走看看