码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习-初学入门3

时间:2019-09-03 19:27:35      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:span   write   utf-8   sub   时间   rda   系统   str   app   

一 集合

1.集合是无序不重复的

2.集合操作:

(1)去列表交集

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Authon:Jerry Z

list_1 = set([1,3,4,5,6])
list_2 = set([1,5,11,23])

print("并集")
print(list_2.union(list_1))
print(list_1 | list_2)

print("交集")
print(list_1.intersection(list_2))
print(list_2 & list_1)

print("差集") # in list1 not in list2
print(list_2.difference(list_1))
print(list_2 - list_1)
print(list_1.difference(list_2))
print(list_1 - list_2)


print("对称差集")
print(list_2.symmetric_difference(list_1))
print(list_1 ^ list_2)

print("子集")
list_3 = set([3,6])
print(list_3.issubset(list_1))
print("父集")
print(list_1.issuperset(list_3))

print("是否有交集的方法,没有交集放回True否则返回False")
print(list_2.isdisjoint(list_3))
print(list_3.isdisjoint(list_1))

for x in list_1:
    print(x)

print("基本操作")
print("添加")
list_4 = set([9,1,2,3,4])
list_4.add(5) #添加一项
print(list_4)

list_4.update([6,7,8]) #添加多项
print(list_4)

print("使用remove可以删除一项,这项不存在的报错")
list_4.remove(8)
print(list_4)

print("len(s)长度")
print("x in s测试x是不是在s中,x可以是列表 字符 元组等")

print("pop删除任意一个")
list_4.pop()
print(list_4)

 

二 文件操作

1.文件操作流程:打开->操作->关闭

2.文件基本操作:

print("文件基本操作")
# data = open("yesterday",encoding="utf-8").read()
# # data = open("yesterday").read()
# print(data)

f = open("yesterday","r",encoding="utf-8") #文件句柄
data = f.read()#第一遍读
data2 = f.read()# 此处是从第一遍读取结尾的地方开始读取。所以文件第一遍读取之后再读取就没内容了
print(data)
print("---------data2----%s---" % data2)
f.close()

print("写")
f2 = open("yesterday2","a",encoding="utf-8") #文件句柄
f2.write("我爱北京天安门\n")
f2.write("天安门上太阳升\n")
# data3 = f2.read() #不能读取
# print("-------data3:",data3)
f2.close()


print("*****************")
‘‘‘
模式:
    w:是重新创建一个,之前的会覆盖
    a:a = append,往后追加不覆盖原来内容,也不能读取
‘‘‘

f3 = open("yesterday","r",encoding="utf-8")
# for index,line in enumerate(f3.readlines()):
#     if index == 9:
#         print("------------------不打印----------------")
#         continue
#     print(line.strip())
‘‘‘
enumerate缺陷是先读到内存才开始循环,读取到内存需要时间等待,如文件太大会超过内存
所以enumerate只适合处理小文件
‘‘‘

# print("-----------*******------------")
# list1 = f3.readlines()
# for index in range(len(list1)):
#     if index == 9:
#         print("------------------不打印----------------")
#         continue
#     print(list1[index].strip())
#

print("高效取文件每行的方法")
print("-----------*******------------")
count = 0
for line in f3:
    if count == 9:
        print("------------------不打印----------------")
        count += 1
        continue
    print(line.strip())
    count += 1
f3.close()


f4 = open("yesterday","r",encoding="utf-8")
print("tell返回读取文件时候的指针,打印当前位置")
print(f4.readline())
print(f4.tell())
print("seek将指针设置指定位置")
f4.seek(4)
print(f4.readline())

print("打印编码格式")
print(f4.encoding)
print("fileno系统处理文件的接口编号")
print(f4.fileno())

print("seekable是否可移动指针")
print(f4.seekable())

print("readable文件是否可读")
print(f4.readable())

print("flush刷新存入硬盘")
print(f4.flush())


f5 = open("yesterday3","a",encoding="utf-8")
f5.seek(5)
# f5.truncate(10)#从文件开头截取 后面的删掉,seek失效

print("********文件的修改********")
print("r+:读写")
f6 = open("yesterday3","r+",encoding="utf-8")
print(f6.readline())
print(f6.readline())
print(f6.readline())
f6.write("\nr+读写操作\n")


print("w+:写读")
f7 = open("yesterday4","w+",encoding="utf-8")
f7.write("aaaa写读操作\n")
f7.write("aaaa写读操作\n")
f7.write("aaaa写读操作\n")

print(f7.tell())
f7.seek(10)
print(f7.readline())
f7.write("aaaaaaaaaaaaa\n")
f7.close()

print("a+:追加读写")
print("rb:二进制格式读文件。使用的情况:1网络传输 socket等 python3 只用用二进制 python2还可以用字符 2 打开对应的文件。二进制文件操作用对应的操作符")
f8 = open("yesterday4","rb")
print(f8.readline())
print(f8.readline())
print(f8.readline())

f9 = open("yesterday5","wb")
f9.write("Hello binary".encode())


print("文件的修改")
"""
1 vim
2 读取文件重新存入新的文件
"""


f10 = open("yesterday","r",encoding="utf-8")
f11 = open("yeterday6","w",encoding="utf-8")

for line in f10:
    if "愚蠢的游戏" in line:
        # line = "我享受生命 视其为快乐的游戏\n"
        line = line.replace("我戏弄生命 视其为愚蠢的游戏","我享受生命 视其为快乐的游戏")
    f11.write(line)

f10.close()
f11.close()



print("with数据,可以避免文件忘记关闭,自动关闭忘记")

with open("yesterday7","w",encoding="utf-8") as f1 ,        open("yesterday8", "w", encoding="utf-8") as f2 :
    f1.write("dddd")
    f2.write("dddd")

 

Python学习-初学入门3

标签:span   write   utf-8   sub   时间   rda   系统   str   app   

原文地址:https://www.cnblogs.com/zhenhua37/p/11454949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!