@breakerthb
2017-06-07T05:28:41.000000Z
字数 1293
阅读 1431
Python
if __name__=="__main__":
print(abc)
import sys;
sys.path
添加寻找库的目录
import sys
sys.path.append("../Lib")
type(a)
import os
os.getcwd()
os.chdir('....')
file = open('sketch.txt')
或
file = open('sketch.txt', 'w')
file.close()
content = file.readline()
print(content)
file.write('XXXX')
seek(0) # 参数为行号
file.seek(0)
for line in file:
print(line)
if os.path.exists('sketch.txt'):
file = open('sketch.txt')
PS:
在python3中,可以通过pickle模块进行数据的序列化保存。
ref:http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html
# abc : XXXXXX
(a, b) = line.split(":")
print(a)
print(b)
如果有的行存在多个分号:
(a, b) = line.split(":", 1)
如果有的行不存在分号:
pos = line.find(':') # 返回-1表示没找到
b = b.strip()
a = 'hello word'
word替换为python
a.replace('word','python')
import re
strinfo = re.compile('word')
b = strinfo.sub('python',a)
print b
file = open('sketch.txt')
try:
for line in file:
(a, b) = line.split(':', 1)
print(a)
print(b)
except:
pass
finally:
file.close()
另外:
try:
with open('out.txt', "w") as fileOut:
for item in man:
fileOut.write(item)
except:
print("output error")
用with打开文件可以不用考虑关闭
data.sort()
data1 = sorted(data)
反向排序
data.sort().reverse()
或者按顺序从尾部pop()
pop() #读出并删除末尾元素
集合中的元素会自动忽略掉重复的。
data = [a, b, c, a]
data1 = set(data)
把一个列表中的所有数据按照一定规则处理之后生成一个新的列表
相当于结构体或map