首页 > 编程语言 > 详细

我的自学Python之路(第二阶段Day5)

时间:2018-06-23 21:47:41      阅读:174      评论:0      收藏:0      [点我收藏+]

1.文件读写

技术分享图片
# f = open(‘shang‘,mode=‘r‘,encoding=‘utf-8‘)
# content = f.read()
# print(content,type(content))
# f.close()


# f = open(‘shang‘,mode=‘rb‘,)
# content = f.read()
# print(content,type(content))
# f.close()

# f = open(‘shang‘,mode=‘w‘,encoding=‘utf-8‘)
# content = f.write(‘hhhhhhkkkkk‘)
# print(content, type(content))
# f.close()

# f = open(‘shang‘,mode=‘wb‘)
# content = f.write(‘的技术开发‘.encode(‘gbk‘))
# print(content,type(content))
# f.close()

# f = open(‘shang‘,mode=‘a‘,encoding=‘gbk‘)
# content = f.write(‘111111111222333‘)
# print(content)
# f.close()

#
# f = open(‘shang‘,mode=‘ab‘)
# content = f.write(‘5566‘.encode(‘utf-8‘))
# print(content)
# f.close()

# f = open(‘shang‘,mode=‘r+‘)
# f.write(‘5566‘)
# print(f.read())
# f.close()

# f = open(‘shang‘,mode=‘w+‘)
# f.write(‘556677‘)
# f.seek(0)
# print(f.read())
# f.close()

# f = open(‘shang‘,mode=‘w+b‘)
# print(f.read())
# f.write(‘5566‘.encode(‘utf-8‘))
# f.close()


# f = open(‘shang‘,mode=‘a+‘)
# f.write(‘556677‘)
# f.seek(0)
# print(f.read())
# f.close()


# f = open(‘shang‘,mode=‘a+b‘)
# f.write(‘556677‘.encode(‘utf-8‘))
# f.seek(0)
# print(f.read())
# f.close()
View Code

 2.f.seek()是按照字节来调整光标,f.read()是按照字符来读取

技术分享图片
# f = open(‘shang‘,mode=‘w‘,encoding=‘utf-8‘)
# f.write(‘手机号更深刻的‘)
#
# f.close()

f = open(shang,mode=r+,encoding=utf-8)
# print(f.read(3))

f.seek(6)
print(f.read())

f.close()
View Code

3.研究发现不同的电脑情况不同,有的电脑安装pycharm后不可以用,提示没有下载JDK,经网上查询,在Java的网站oracle上下载JDK,并且配置好环境变量才可以用,文章已经收藏到CSDN。

4.补充学习

技术分享图片
# with open(‘shang‘, mode=‘r+‘, encoding=‘utf-8‘) as f:
#     f.seek(3)
# #     # print(f.readable())
# #     # print(f.tell())告诉你光标的位置
# #     # line = f.readline()# 一行一行的读
# #     f.read()
#     # f.truncate(4)truncate() 方法用于截断文件,如果指定了可选参数 size,则表示截断文件为 size 个字符。 如果没有指定 size,则从当前位置起截断;截断之后 size 后面的所有字符被删除。
#
# #     line = f.readlines()# 每一行当成列表中的一个元素,添加到list中
#
# with open(‘shang‘, mode=‘r+‘, encoding=‘utf-8‘) as f:
#     for i in f:
#         print(i)
View Code

5.写入文件的登录注册

技术分享图片
# 登录注册
username = input(请输入你要注册的姓名:)
password = input(请出入你的密码:)
with open(info,mode=w,encoding=utf-8) as f1:
     f1.write({}\n{}.format(username,password))
print(恭喜注册成功)
li = []
i = 0
while i < 3:
    usn = input(请输入用户名:)
    psd = input(请输入用户名:)
    with open(info,mode=r+,encoding = utf-8) as f2:
        for line in f2:
            li.append(line)
    if usn == li[0] and psw == li[1]:
        print(登录成功)
    else:
        print(用户名和密码错误)
    i += 1
View Code

 6.解包

技术分享图片
# 解包
a,b,c = 1,2,3
print(b)
a,b,c = [1,2,3]
print(b)
a,b,c = (1,2,3)
print(b)
a,b,c = {1:1,9:2,3:3}
print(b)
View Code

7.文件删除,重命名,函数与返回值

返回值
#返回值的3种情况
# 没有返回值 —— 返回None
# 不写return
# 只写return:结束一个函数的继续
# return None —— 不常用
# 返回1个值
# 可以返回任何数据类型
# 只要返回就可以接收到
# 如果在一个程序中有多个return,那么只执行第一个
# 返回多个值
# 用多个变量接收:有多少返回值就用多少变量接收
# 用一个变量接收: 得到的是一个元组

技术分享图片
# import os
# os.remove(‘info‘)
# os.rename(‘shang‘,‘chun‘)
# with open(‘chun‘,mode=‘r‘,encoding=‘utf-8‘) as f:
#     for i in f:
#         print(i)

# def fun():
#     return 1,2,3
# f= fun()
# print(f)
View Code

 8.今日练习题

1. 文件a.txt内容:每一行内容分别为商品名字,价钱,个数。
apple 10 3
tesla 100000 1
mac 3000 2
lenovo 30000 3
chicken 10 3
通过代码,将其构建成这种数据类型:[{‘name‘:‘apple‘,‘price‘:10,‘amount‘:3},{‘name‘:‘tesla‘,‘price‘:1000000,‘amount‘:1}......] 并计算出总价钱。


2,有如下文件:
alex是老男孩python发起人,创建人。
alex其实是人妖。
谁说alex是sb?
你们真逗,alex再牛逼,也掩饰不住资深屌丝的气质。
将文件中所有的alex都替换成大写的SB。

我的自学Python之路(第二阶段Day5)

原文:https://www.cnblogs.com/shangchunhong/p/9216778.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!