Python基础(四)

  • open函数
    open(name[,mode[,buffering[,encoding]]])
    mode(访问模式):
    -1 : r(只读) w(只写,若文件存在则覆盖) a(追加) rb(二进制只读) wb(二进制只写)
    -2 : ab(二进制追加) r+,w+,a+(都是读写) rb+,wb+,ab+(都是二进制读写)
    r+,w+,a+区别
    r+ :文件的指针调到文件头部
    w+:如果文件不存在直接创建,存在覆盖源文件
    a+ :文件的指针调到文件末尾
  • 打开关闭文件
    open
#打开文件
file1 = open('python.txt','r')
print(file1)

#读写操作

#关闭文件
file1.close() 

with open完成后自己自动关闭

with open('python.txt','r',encoding = 'utf-8') as file1:
    content = file1.read()
    print(content)
  • 读文件
    read(num):读取文件内容,num表示指定长度,如果没有则读取所有数据
file1 = open('python.txt','r',encoding = 'utf-8')
content = file1.read() #读取数据保存在content变量当中
print(content)

readlines()按行读取,返回一个列表,每一行的数据为一个元素,换行也会转换成str格式即'\n'

file1 = open('python.txt','r',encoding = 'utf-8')
content = file1.readlines() #逐行读取内容
print(content)
file1.close()
  • 逐行读取
  1. open结合for循环
file1 = open('python.txt','r',encoding = 'utf-8')
i = 1
for line in file1:
    #没有使用read,像迭代器一样节省空间
    print('这是第%d行:%s'%(i,line))
file1.close()
  1. with结合for循环
with open('python.txt','r',encoding = 'utf-8') as file1:
    i = 1
    for line in file1:
    #没有使用read,像迭代器一样节省空间
        print('这是第%d行:%s'%(i,line))
        i += 1
  • 写入文件write
#以写的方式打开一个文件
file1 = open('python.txt','w',encoding = 'utf-8') #覆盖源文件
#file1 = open('python.txt','a',encoding = 'utf-8') #追加
content = 'hello'
file1.write(content)
file.close()
with open('python.txt','w',encoding = 'utf-8') as file1:
#with open('python.txt','a',encoding = 'utf-8') as file1:
    content = 'hello'
    file1.write(content)
  • 常用函数
    flush 把缓冲区内容写入硬盘
    tell() 查看文件指针
file1 = open('python.txt','r',encoding = 'utf-8')
str = file1.read(5) #读取数据保存在content变量当中
print('当前读取的数据是:'+str)

#查看当前指针位置
position = files.tell()
print('当前位置是:',position)

file1.close()

seek(offset[,whence])设置指针位置
offset是偏移量,whence有三个变量:0,1,2
0:从头开始算
1:从当前位置开始算
2:从末尾开始算

  • 文件夹的操作,要import os模块
import os
#获取当前路径
print(os.getcwd())

#列出当前(默认的)或指定目录的文件和文件夹
print(os.listdir('F:\python3.7\\))

#判断是否是一个文件
print(os.path.isfile('1.txt'))

#判断文件是否存在
print(os.path.exists('1.txt'))

#重命名文件
os.rename('1.txt','2.txt')

#删除文件
os.remove('2.txt')

#将目录和文件分割成2个元素,作为列表输出
os.path.split(F:\python3.7\1.txt)

#创建目录
os.mkdir('py')

#删除目录
os.rmdir('py')
  • 异常处理try except else finally
  • 简单异常处理
try:
    print(a) #如果有错,就会捕获到异常
except ValueError:
    print('变量未定义') #ValueError对异常的处理
except NameError:
    print('变量未定义') #NameError对异常的处理
    
#捕获异常的具体信息
try:
    print(a)
    file = open('a.txt','r')
except (NameError,FileNotFoundError) as e: #异常元组,若不知道什么异常可以用基类Exception
    print(e) #打印异常的具体信息,捕捉第一个异常就输出了
  • else没有异常时执行的语句
  • finally不管有没有异常都执行
  • 常用模块—time模块
import time

print(time.altzone) #返回格林威治西部的夏令时地区的偏移秒数

print(time.asctime()) #默认返回可读形式的当前时间
print(time.asctime((2017,12,12,12,12,12,3,340,1))) #返回可读形式的时间,感觉没啥用啊

print(time.gmtime()) #返回时间元组,格林威治时间的元组
print(time.localtime()) #返回本地时间元组

print(time.clock()) #返回进程时间,以秒为单位记时间戳

print(time.ctime()) #获取当前时间
print(time.time()) #返回当前时间的时间戳,从今年1月1日0点到现在的秒数

for i in range(3):
    print(1)
    time.sleep(2) #睡眠两秒

格式化时间:时间戳->时间元组->时间字符串

import time
times = time.time() #获取当前时间戳
formatTime = time.localtime(times)
print(time.strftime('%Y-%m-%d %H:%M:%S'.formatTime))

#time.strptime将时间字符串转换为时间元组
times = '2017-12-12 12:12:12'
formatTime = time.strptime(times,'%Y-%m-%d %H:%M:%S')
print(formatTime)

#mktime将时间元组转换为时间戳
print(time.mktime(formatTime))
  • 三天前的时间
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()-3*24*60*60)))
  • 进程与线程
import time
import threading

#单线程
def music(name,loop):
    for i in range(loop):
        print('listen music %s %s'%(name,time.ctime()))
        time.sleep(1)
def movie(name,loop):
    for i in range(loop):
        print('watch movie %s %s'%(name,time.ctime()))
        time.sleep(1)  

if __name__ = '__main__':
    music('music1',3)
    movie('movie2',2)
    print('end time %s'%time.ctime())

#创建多线程:假多线程,不建议使用
t1 = threading.Thread(target = music,args = ('music1',3))
t2 = threading.Thread(target = movie,args = ('movie2',2))     

if __name__ = '__main__':
    #守护主线程,主线程结束杀死子线程
    t1.setDaemon(True)
    t2.setDaemon(True)
    
    #启动线程
    t1.start()
    t2.start()
    
    #对主线程进行阻塞,等所有的子线程运行结束,再运行主线程
    t1.join()
    t2.join()
    
    print('end time %s'%time.ctime())
  • 全局解释锁GIL
#加锁
balance = 0
def change(n):
    global balance
    balance += n
    balance -= n
    
lock = threading.Lock() #获取线程锁
def run_thread(n):
    for i in range(100000):
        #获取锁
        lock.acquire()
        try:
            change(n)
        finally:
            #释放锁
            lock.release()
        
t1 = threading.Thread(target = run_thread,args = (4,))
t2 = threading.Thread(target = run_thread,args = (8,))

t1.start()
t2.start()

t1.join()
t2.join()

print(balance)
  • 多进程:用multiprocessing代替Thread
    单进程
import time

def work1(f,n):
    print('work1 start')
    for i in range(n):
        with open(f,'a') as fs:
            fs.write('hello\n')
            time.sleep(1)
    print('work1 end')
    
def work2(f,n):
    print('work2 start')
    for i in range(n):
        with open(f,'a') as fs:
            fs.write('world\n')
            time.sleep(1)
    print('work2 end')
    
if __name__ = '__main__':
    work1('1.txt',3)
    work2('1.txt',3)

多进程加锁

import time
import multiprocessing

def work1(f,n,lock):
    print('work1 start')
    lock.acquire()
    for i in range(n):
        with open(f,'a') as fs:
            fs.write('hello\n')
            time.sleep(1)
    print('work1 end')
    lock.release()
    
def work2(f,n,lock):
    print('work2 start')
    lock.acquire()
    for i in range(n):
        with open(f,'a') as fs:
            fs.write('world\n')
            time.sleep(1)
    print('work2 end')
    lock.release()
    
if __name__ = '__main__':
    lock = multiprocessing.Lock()
    p1 = multiprocessing.Process(target=work1,args=('1.txt',3,lock))
    p2 = multiprocessing.Process(target=work2,args=('1.txt',3,lock))
    p1.start()
    p1.start()
  • 进程池pool
import os
import multiprocessing
import time

def work(n):
    print('run work(%s),work id %s'%(n,os.getpid()))
    time.sleep(5)
    print('work(%s) stop,work id %s'%(n,os.getpid()))

if __name__ = '__main__':
    print('parent process %s.'% os.getpid())
    #创建进程池
    p = multiprocessing.Pool(3)
    for i in range(5):
        #创建5个进程
        p.apply_async(work,args=(i,))
        p.close()
        p.join()
  • pandas数据分析包
    引入约定:
    drom pandas import Series,DataFrame 系列,帧
    import pandas as pd
    Series:类似一维数组的对象,索引值是可以重复的
    DataFrame :表格型,没列可以是不同的数据类型,既有行索引也有列索引
  • 通过一维数组创建Series
import pandas as pd

#通过一维数组创建Series
ser01 = pd.Series([1,2,3,4])
ser01
print(ser01.dtype) #获取类型,输出: int32
print(ser01.values) #获取值,输出: [1 2 3 4]
print(ser01.index) #获取索引,输出: RangeIndex(start=0,stop=4,step=1)

#设置索引 通过 index 属性
ser01.index = ['a','b','c','d']
ser01

#也可以在创建时设置属性
ser02 = pd.Series(np.Series(np.array([1,2,3,4]),dtype = np.float64,index = ['a','b','c','d'])

#通过字典的方式创建
ser02 = pd.Series({'a':10,'b':20,'c':30})
ser02

#获取Series
print(ser02['a']) #通过key,输出:10
print(ser02[0]) #通过坐标,输出:10
print(ser02[0:2]) #类似切片,含左不含右,输出2对数据
print(ser02['a':'c']) #类似切片,但输出3对数据
  • Numpy的运算Series基本都可以用
import pandas as pd
ser01 = pd.Series([1,2,3,4])
ser01+1 #每个数都+1,很简单
  • Series缺失值处理
ser01 = pd.Series([1,2,3])
ser02 = pd.Series(ser01,index=['a','b','c','d'])
ser02 #缺失的数据用NaN来代替
ser02[pd.isnull(ser02)]
ser02[pd.notnull(ser02)] #过滤掉缺失值
  • Series自动对齐:有的自动对齐,没有的自动NaN
ser01 = pd.Series([1,2,3,4],index = ['a','b','c','d'])
ser02 = pd.Series([10,20,30,40],index = ['e','a','b','f'])
print(ser01+ser02)
'''
    输出:
    a   21.0
    b   32.0
    c   NaN
    d   NaN
    e   NaN
    f   NaN
'''
  • Seriesname属性
    ser01.name 总名字
    ser01.index.name 第一列索引名
  • DataFrame创建
    df01 = pd.DataFrame(['joe','susan','anne'],[70,80,90],index = ['one','two','three'],columns = ['name','score'])
    通过字典方式创建
df01 = pd.DataFrame({
        'name':['joe','susan','anne'],  #key 变成列索引
        'age':[18,19,20],
        'class':3
},index = ['one','two','three'])
print(df01) #输出一个表
  • DataFrame 数据处理
    df01['name'] 通过列索引获取数据
    df01['address'] = ['shanghai','beijing','hangzhou'] 添加列数据
    df01.pop('address') 列删除
    df01.ix['one']行获取,古老的方法
    df01.loc['two']行获取
    df01.ix['four'] = ['batman',25,4]行修改,没这行就添加,有这行就修改
    df02 = df01.drop('four')行删除
  • pandas基本操作
import pandas as pd

#读取文件
df01 = pd.read_csv('data1.csv') #读取csv
print(df01)

df02 = pd.read_excel('data1.xlsx') #读取excel
print(df02)

df03 = pd.read_csv('data1.txt',sep = ';',header = None) 
#读取txt,sep表示用什么分割,header=None表示不用第一排作为列索引
print(df03)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容