一、字符串方法完善

s='abcdef'

users = ['username','user2','user3']
# username,user2,user3

# res = ','.join(users)#1、把list变成了字符串 2、把list里面每一个元素用逗号连接起来
# print(res)
# print(s.find('z'))#返回-1
# print(s.index('z'))#报错
# print(s.count('z'))
# print('0'.isdigit())#判断是否为正整数
# print(s.islower())
# print(s.isupper())
print('acbe123'.isalnum()) #判断字符串里面有英文或者有数字。
#aaaAfd true
#1223432 true
#acas12323 true
#sdf&*( false

print('acbe'.isalpha()) #只能是字母
#都是字母才返回ture

# print(s.isspace())#判断是否的空格
# s.splitlines()#以换行符分割字符串
#都是找元素的下标,先分别用他们去找存在的元素,再找不存在的


# l = list(range(1,110))
# new_l = []
# for i in l:
# si=str(i)
#
# # if len(si)==1:
# # new_i = '00'+si
# # elif len(si) == 2:
# # new_i = '0'+si
# # else:
# # new_i = si
# new_l.append(si.zfill(5))
# print(new_l)
# 001 ,002,003 012,100 ,101

import string
print(string.ascii_lowercase)
print(string.ascii_uppercase)
print(string.digits)
print(string.punctuation)
print(string.ascii_letters)


二、文件读写
# r  只读,打开文件不存的话,会报
# w 只写,会清空原来文件的内容
# a 追加写,不会请求,打开的文件不存在的话,也会帮你新建一个文件

# r+ 读写模式
# w+ 写读模式
# a+ 追加读模式

# rb wb ab+
# r+和w+分别试一下能不能读写,r+打开不存在的文件是否会报错
# f = open('users2.txt','a+',encoding='utf-8')
# f.seek(0)
# print(f.read())
# f.write('a+模式')
# f.write('hahaha')
# print('读',f.read())#获取到文件里面所有的内容
# print(f.readlines())#获取到文件里面所有的内容
# print(f.readline())#读取一行
# print(f.readline())
# print(f.readline())


# a=['username1,12345\n','username2,123456\n']
# # for i in a:
# # f.write(i+'\n')
# u='abc,123'
# f.writelines(u)

三。修改文件内容
#1、简单、粗暴直接的
# f = open(r'C:\Users\nhy\Desktop\file.txt',encoding='utf-8')
# res = f.read().replace('一点','二点')
# f.close()
# f = open(r'C:\Users\nhy\Desktop\file.txt',mode='w',encoding='utf-8')
# f.write(res)
# f.flush() #立即把缓冲区里面的内容,写到磁盘上
# f.close()

# f = open('file.txt','a+',encoding='utf-8')
# f.seek(0)
# res = f.read().replace('你','NI')
# f.seek(0)
# f.truncate() #清空文件里面的内容
# f.write(res)
# f.close()
#缓冲区 a
import os
# f = open('file.txt',encoding='utf-8')
# f2 = open('file.txt.bak','w',encoding='utf-8')
# for line in f:
# new_line = line.replace('一点','二点')
# f2.write(new_line)
# f.close()
# f2.close()
# os.remove('file.txt')
# os.rename('file.txt.bak','file.txt')

with open('file.txt',encoding='utf-8') as f, open('file.txt.bak','w',encoding='utf-8') as f2:
for line in f:
new_line = line.replace('二点','一点')
f2.write(new_line)

os.remove('file.txt')
os.rename('file.txt.bak','file.txt')

四、处理json
# json通用的数据类型,所有的语言都认识
# k-v { }
#json串是字符串


s='''
{
"error_code": 0,
"stu_info": [
{
"id": 309,
"name": "小白",
"sex": "男",
"age": 28,
"addr": "河南省济源市北海大道32号",
"grade": "天蝎座",
"phone": "18512572946",
"gold": 100
},
{
"id": 310,
"name": "小白",
"sex": "男",
"age": 28,
"addr": "河南省济源市北海大道32号",
"grade": "天蝎座",
"phone": "18516572946",
"gold": 100
}
]
}

'''
import json



# res = json.loads(s) #json串(字符串),转成字典
# print(res)
# print(res.keys())
# print(type(res))
# #abc,123
# #cba,456
stus = {'xiaojun':'123456','xiaohei':'7891','tanailing':'11111'
,'海龙':'111'}
# res2 = json.dumps(stus,indent=8,ensure_ascii=False)
# print(res2)
# with open('stus.json','w',encoding='utf-8') as f:
# f.write(res2)

# f = open('stus.json',encoding='utf-8')
# content = f.read()
# user_dic = json.loads(content)
# print(user_dic)


# f = open('stus.json',encoding='utf-8')
# user_dic = json.load(f)
# print(user_dic)

stus = {'xiaojun':'123456','xiaohei':'7891','tanailing':'11111'
,'海龙':'111'}

res2 = json.dumps(stus,indent=8,ensure_ascii=False)
print(res2)
with open('stus.json','w',encoding='utf-8') as f:
f.write(res2)


f = open('stus2.json','w',encoding='utf-8')
json.dump(stus,f,indent=4,ensure_ascii=False)

#如果你要把字典写到文件里面的

五、函数
# 函数、方法,实现特定功能的一坨代码
# 提高代码的复用性
import json
# with open('a.txt') as f:
# res = json.load(f)
#
# with open('b.txt') as f:
# res = json.load(f)
#
# with open('c.txt') as f:
# res = json.load(f)

def my():
print('函数')
#函数必须得调用才会执行


#函数里面定义的变量,就是局部变量,局部变量只在函数里面可以使用
#出了函数外面之后,就不能使用了。

def get_file_content(file_name):#形参,形式参数
#入参:传入一个文件名
#返回值:文件内容转成字典,返回
with open(file_name,encoding='utf-8') as f:
res = json.load(f)
return res
#一个函数只做一件事
abc = get_file_content('stus.json') #实参,实际参数
# print(abc)
def write_file(filename,content):
with open(filename,'w',encoding='utf-8') as f:
json.dump(content,f,ensure_ascii=False,indent=4)
# f.write(json.dumps(content))

d={'name':'nhy','sex':'nan'}
d2={'aaa':'xxx','a':1}
write_file('nhy.json',d)
write_file('yanfan.json',d2)



#不要把一个函数里面的代码写的特别多