day1--心得
info = '''
--------------------info of %s----------------
name: %s
age: %s
job: %s
------------------------------------------ ''' % (name,name,age,job)
print(info) info2 = '''
--------------------info2 of {_name}----------------
name: {_name}
age: {_age}
job: {_job}
------------------------------------------ ''' .format(_name = name,
_age = age,
_job = job ) print(info2)
以上有2中方式
------------------------------------------------------
输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:
#!/usr/bin/env python
# -*- coding: utf-8 -*- import getpass # 将用户输入的内容赋值给 name 变量
pwd = getpass.getpass("请输入密码:") # 打印输入的内容
print(pwd)
strip() 方法用于移除字符串头尾指定的字符(默认为空格)
password = getpass.getpass("input your password\n:>>>").strip()
-----------------------------------------
py2与3的详细区别
Old: print "The answer is", 2*2 New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: print() # You must call the function!
Old: print >>sys.stderr, "fatal error" New: print("fatal error", file=sys.stderr)
Old: print (x, y) # prints repr((x, y))
New: print((x, y)) # Not the same as print(x, y)!
库改名字了:
Old Name |
New Name |
_winreg |
winreg |
ConfigParser |
configparser |
copy_reg |
copyreg |
Queue |
queue |
SocketServer |
socketserver |
markupbase |
_markupbase |
repr |
reprlib |
test.test_support |
test.support |
----------------------------------------------
Python注释:
当行注视:# 被注释内容
多行注释:""" 被注释内容 """
---------------------------------------------
python在linux下的补全
#!/usr/bin/env python
# python startup file
import sys
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter for Linux
ocalhost:~ jieli$ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab
上面自己写的tab.py模块只能在当前目录下导入,如果想在系统的何何一个地方都使用怎么办呢? 此时你就要把这个tab.py放到python全局环境变量目录里啦,基本一般都放在一个叫 Python/2.7/site-packages 目录下,这个目录在不同的OS里放的位置不一样,用 print(sys.path) 可以查看python环境变量列表
--------------------------------------------
range:
for i in range(0,10,2):
if i <5:
print("loop",i)
else:
continue
print("hehe")
--------------------------------------------
python中的三个读read(),readline()和readlines()
Python 将文本文件的内容读入可以操作的字符串变量非常容易。文件对象提供了三个“读”方法: .read()、.readline() 和 .readlines()。每种方法可以接受一个变量以限制每次读取的数据量,但它们通常不使用变量。 .read() 每次读取整个文件,它通常用于将文件内容放到一个字符串变量中。然而 .read() 生成文件内容最直接的字符串表示,但对于连续的面向行的处理,它却是不必要的,并且如果文件大于可用内存,则不可能实现这种处理。
.readline() 和 .readlines() 非常相似。它们都在类似于以下的结构中使用:
Python .readlines() 示例
fh = open( 'c:\\autoexec.bat') for line in fh.readlines(): print line.readline() 和 .readlines()之间的差异是后者一次读取整个文件,象 .read()一样。.readlines()自动将文件内容分析成一个行的列表,该列表可以由 Python 的 for... in ... 结构进行处理。另一方面,.readline()每次只读取一行,通常比 .readlines()慢得多。仅当没有足够内存可以一次读取整个文件时,才应该使用.readline()。
写:
writeline()是输出后换行,下次写会在下一行写。write()是输出后光标在行末不会换行,下次写会接着这行写
通过readline输出,对于比较大的文件,这种占用内存比较小。
#coding:utf-8 f = open('poem.txt','r')
result = list()
for line in open('poem.txt'):
line = f.readline()
print line
result.append(line)
print result
f.close()
open('result-readline.txt', 'w').write('%s' % '\n'.join(result))
#coding:utf-8
'''cdays-4-exercise-6.py 文件基本操作
@note: 文件读取写入, 列表排序, 字符串操作
@see: 字符串各方法可参考hekp(str)或Python在线文档http://docs.python.org/lib/string-methods.html
''' f = open('cdays-4-test.txt', 'r') #以读方式打开文件
result = list()
for line in f.readlines(): #依次读取每行
line = line.strip() #去掉每行头尾空白
if not len(line) or line.startswith('#'): #判断是否是空行或注释行
continue #是的话,跳过不处理
result.append(line) #保存
result.sort() #排序结果
print result
open('cdays-4-result.txt', 'w').write('%s' % '\n'.join(result)) #保存入结果文件
两个字典合并方法:
最新文章
- wifi reaver
- Struts2的一个问题: 找不到struts.xml的路径问题
- DEEP LEARNING IS THE FUTURE: Q&;A WITH NAVEEN RAO OF NERVANA SYSTEMS
- hdoj1847(博弈论)
- E - Swap - hdu 2819(简单二分图匹配)
- Day1_算法分析方法
- 跳转表C语言,不比redis版本号
- C语言和go语言之间的交互
- SVG绘制loading效果
- Python之多进程篇
- centos 6.5 安装 tomcat8 及性能优化_虚拟主机
- springboot项目简单启动脚本
- kernel中,dump_stack打印调用栈,print_hex_dump打印一片内存,记录一下
- 如何新增一个ssh-key文件
- 雷林鹏分享:XML 总结 下一步学习什么呢?
- node启动时候报错 Error: Cannot find module &#39;express&#39;
- 继承方法-->;原型的相互引用
- jsonp promise封装
- Elasticsearch + logstash + kibana 配置
- UESTC - 594 我要长高