1.txt
1.1 txt读文件操作
with open('test.txt',encoding='utf-8') as txt_obj:
line=txt_obj.readline()#一行一行的读取代码
while line:
mingzi=line.rstrip('\n').replace('\xa0',' ').split(' ')[1:]#对字符串进行处理操作
print(mingzi)
line=txt_obj.readline()
txt_obj.close()
注:读写中文字符需要加encoding属性
with open('test.txt',encoding='utf-8') as txt_obj
1.2 txt写文件操作
write_file = open("test1.txt", "w")
content=123
write_file.writelines(str(content))#字符形式能成功写入,取出也为字符型
write_file.close()
2.excle
2.1 excle读操作
import xlrd
data=xlrd.open_workbook('d://rfid/rfid.xls')
table=data.sheets()[0]#选择第一个表格
for index in range(2,81):
search=table.cell(index, 6).value#行号,列号,和值
print(search)
2.2 excle写操作
import xlwt
wd=xlwt.Workbook(encoding='ascii')
ws=wd.add_sheet('wg')#添加一个sheet
ws.write(0,0,label='hh')#行号,列号,写入的值
wd.save('d://rfid/test2.xls')#保存的路径和文件名