python文件处理
列表
元组
字典
函数
练习程序
###########################################python文件处理########################################################
-------创建文件-------
f = file('myfile.txt','w') >>>(w,r,a,w+,r+,a+,(以字符串方式)wb,rb(以二进制方式))
f.write("hello world!")
f.close() >>>不关闭,当其他程序打开时会报错。
-------遍历文件内容-------
a = file('user_info.txt')
for line in a.readlines():
print line,
a.close
-------追加-------
f = file('test.txt','a')
f.write("append to the end")
f.close
-------文件内容替换-------
for line in fileinput.input("filepath",inplace=1) >>>inplace=0或者不写就只会打印,不会写入硬盘
line = line.replace("oldtext","newtext")
print line,
例:
向文件内写入:
import time
f = file('f_file','w')
for i in range(10):
time.sleep(1)
f.write("%s is loops\n" %i)
f.flush() >>>将内存内容写入硬盘
time.sleep(10)
f.close()
向文件内追加:
import time
f = file('f_file','a')
for i in range(11,31):
time.sleep(1)
f.write("%s is loops\n" %i)
f.flush() >>>将内存内容写入硬盘
time.sleep(10)
f.close()
修改文件内容:(不可以随意改,因为python分块读取文件)
f = file('f_file','r') >>>file和open没有区别,open是调用file的。
flle_content = f.readlines() >>>将文件读到内存
print flle_content
文件内容替换:
import fileinput
for line in fileinput.input('f_test.txt',inplace=1,backup='.bak'): >>>inplace=0或者不写就只会打印,不会写入硬盘,backup会把源文件备份,改过的内容在新文件,默认替换所有
line = line.replace("原内容","新内容")
print line,
f.tell() 查看文件读到第几个字符
f.seek(12) 跳到第12个字符
f.truncate() 文件截断,要以r+方式打开。
truncate(n): 从文件的首行首字符开始截断,截断文件为n个字符;无n表示从当前位置起截断;截断之后n后面的所有字符被删除。
with open('f_file','r+') as f 等同于 f = file('f_file','r+')
例:
with open('f_file','r+') as f: >>>这种方法执行成功自动关闭,不用close()
f.seek(10)
f.truncate(20) >>>f.truncate(20)代表从开头位置截20个字符
###############################################python列表#########################################################
什么是list?
list是处理和存放一组数据的列表。
用途:购物列表、工资列表、送礼列表。。。
语法:
ShoppingList = ['car','clothes','Iphone']
name_list = ['gaofubin','songyawei','wangchong','shangfeng'1,2,3,4,5,6,7,8,9,0,songyawei]
>>> name_list[0] >>>列表值从0开始
'gaofubin'
>>> name_list[1]
'songyawei'
如果要调取8的位置
>>>name_list.index(8) >>>调取列表为8的位置
11
>>>name_list.index('wangchong')
2
如果要调取8的位置的值
>>> name_list[name_list.index(8)]
>>>8
向列表插入内容:
>>>name_list.insert(1,'rain') >>向1的位置插入rain
>>>namelist.insert(name_list.index(8),'JACK') >>向8的位置插入JACK
删除列表内容:
name_list.remove('songyawei') >>默认删除找到的第一个
name_list.count('songyawei') >>查看songyawei的个数
name_list.pop(1) >>1为删除列表第一个,默认移除列表最后一个,为空默认删除列表最后一个,-3删除列表倒数第三个。
name_list.reverse() >>内容反转,倒序
name_list.sort() >>排序,先数字,大写,小写
name_list.extend() >>扩展 如a = [99,32,12],name_list.extend(a) 会把a的值追加到name_list后
列表切片
>>> name_list
[0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'dump', 'dump', 'floor', 'gaofubin', 'rain', 'shangfeng', 'songyawei', 'songyawei', 'wangchong']
>>> name_list[2:6]
[3, 4, 5, 6]
>>> name_list[7:]
[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 'dump', 'dump', 'floor', 'gaofubin', 'rain', 'shangfeng', 'songyawei', 'songyawei', 'wangchong']
>>> name_list[:5]
[0, 1, 3, 4, 5]
>>> name_list[-5:] >>>截取倒数5个
['rain', 'shangfeng', 'songyawei', 'songyawei', 'wangchong']
c = [x for x in range(100)]
c[1:99:2] >>>截取1到99之间的奇数
####################################################元组#############################################################
跟列表一样,但内容一旦生成,不可修改
NameList = ('songyawei','gaofubin','shangfeng')
遍历列表
NameList = ['songyawei','gaofubin','shangfeng']
for name in NameList:
print'Your name is',name
####################################################字典#############################################################
>>>待续。。。。