首页 > 代码库 > Python学习笔记(十五)用Python获取本地数据

Python学习笔记(十五)用Python获取本地数据

技术分享

 

技术分享

 

f1 = open(rE:\Python\Data\data1.txt)  #读取data1.txt文件,使用系统默认缓冲区大小, 为了读取快点,使用缓存吧!

 技术分享技术分享

技术分享

f = open(rE:\Python\Data\data2.txt, w)f.write(Hello World !)f.close()f = open(rE:\Python\Data\data2.txt, r)p1 = f.read(5)  # 先读5个字节p2 = f.read()   # 余下的都读出来
f.close()

file_obj.readlines(), file_obj.readline(), file_obj.writelines()

f = open(rE:\Python\Data\data3.txt)cNames = f.readlines()   #把数据从文件一行行读取,返回一个列表print(cNames)f.close()
#结果, readlines()读取的结果中,不去掉换行符[GOOGLE Inc\n, Nicrosoft Corporation\n, Apple Inc.\n, Facebook, Inc.]

技术分享

# Filename:revcopy.pyf1 = open(rE:\Python\Data\data3.txt)   # 读cNames = f1.readlines() for i in range(0, len(cNames)):    cNames[i] = str(i+1) + . + cNames[i]f1.close()f2 = open(rE:\Python\Data\data3_out.txt, w)  # 写f2.writelines(cNames)f2.close()

技术分享

s = Tencent Technology Company Limitedf = open(rE:\Python\Data\data3.txt, a+)f.writelines(\n)f.writelines(s)f.seek(0, 0)            # 把文件指针移到文件首部cNames = f.readlines()print(cNames)f.close()

 

Python学习笔记(十五)用Python获取本地数据