记录下学习python的一些语法


import os
import random
from openpyxl import load_workbook
# file_path = os.path.join('db','test','1111.txt')
# if(os.path.exists(file_path)):
#     f= open(file_path,mode="r")
#     line = f.read()
#     print(line)
#     f.close()

# nPath = "src/images/0401"
# os.makedirs(nPath)

# file_path = os.path.join('db','test')
# if not os.path.exists(file_path):
#     os.mkdir(file_path)
# else:
#     print('文件已经存在')
#     f1_path = os.path.join(file_path,'22.txt')
#     f=open(f1_path,mode="a")
#     f.write('明天要下雨')
#     f.close()
# file_path = os.path.join('db','test')
# for name in os.listdir(file_path):
#     f=open(os.path.join(file_path,name),mode="r")
#     data = f.read()
#     f.close()
#     print(data)
# print(random.randint(1,10))
# chr_list =[]
# for i in range(5):
#     index = random.randint(65,90)
#     zimu = chr(index)
#     chr_list.append(zimu)
# print("".join(chr_list))
# file_path = os.path.join('db','news.xlsx')
# mess_list=[]
# if not os.path.exists(file_path):
#     print('文件不存在')
# else:
#     ws = load_workbook(file_path)
#     sheet = ws.active
#     print(sheet)
#     for row in sheet.iter_rows(values_only=True):
#         mess =row[1]
#         mess_list.append(mess)

# str = random.choice(mess_list)

# total =10000
# num =20
# for i in range(20):
#     score = random.randint(1,11)
#     if score<5:
#         continue
#     else:
#         total=total-1000
#         print(f"{i}的绩效分是{score}")
#         if total==0:
#             break
# name="zhansgan"
# print(len(name))

# num =10
# def getSum():
#     global num
#     num = num-1
#     print(num)

# getSum()
# print(num)

##列表的操作
# class_list = ['zhangsan','lisi','wanger','demo']
# print(class_list.index('lisi')) #方法一 获取列表元素的下标值
# class_list[1]='李四'  #修改列表
# class_list.insert(1,'李四') #指定位置插入一个元素
# class_list.remove('lisi') #删除列表的一个元素
# class_list.append('费四') #在列表最后追加一个元素
# class_list.extend([1,2,3]) #追加一批元素
# del class_list[0] #根据下标删除元素
# class_list.pop()
# print(class_list.count('zhangsan')) #统计元素出现的次数
# print(len(class_list)) #获取列表的长度

# for item in class_list:
#     print(item)

# index=0
# while index<len(class_list):
#     print(class_list[index])
#     index=index+1


## 元组的操作 特点:定义的元祖元素不能随意修改 所以方法比较少 len() index() count()
# ball_list =(1,2,3,4,5)
# print(type(ball_list))


# ##字符串的操作 字符串不能修改删除
# str ="azhansgan lisi wangera"
# # print(str.replace('s','aa')) #替换字符串的字母,返回一个新的字符串,老的字符串不会改变
# # print(str.split(' ')) #根据条件 将字符串切割 返回一个新的列表
# # print(str.strip()) #不写参数去除前后的空格 传入参数返回去除前后传入的参数
# # print(str.count('a')) #获取字符串的某个元素出现的个数
# # print(len(str)) #获取字符串的长度


# stydu_str ="itheima itcast boxuegu"
# print(stydu_str.count('it'))
# print(stydu_str.replace(' ','|'))
# print(stydu_str.split('|'))

#数组的序列操作 [a:b:c] a,b指数组的下角标 c可以为负数 指从后往前去切割
# new_list = [1,2,3,4,5,6]
# print(new_list[1:4]) #[2, 3, 4]
# print(new_list[0:3:2]) #[1,3]
# print(new_list[:]) #[1,2,3,4,5,6]
# print(new_list[::-1])  #全部反转
# print(new_list[5:2:-1]) #[6,5,4] 从后面去切
# print(new_list[::-1])

# test_str="万过薪月方员序程马黑来,nohtyP学"
# print(test_str[9:4:-1]) #黑马程序员

#字典的相关操作

#函数传参 *args可以传递多个参数值
# def getName(*args):
#     return kwargs

# cc = getName('zhangsan','nan','320')
# print(cc)

# def getAge(**args):
#     return args


# aa = getAge(name="zhansgan",sex="男")
# print(aa['name'])


#函数通过参数传递

# def getTotal(a,b):
#     return a+b

# def ptTotal(getTotal):
#     count = getTotal(1,2)
#     return count

# sum = ptTotal(lambda x,y:x+y)
# print(sum)


# print(ptTotal(getTotal))

# f = open('11.txt',mode="r",encoding="utf-8")
# # #1.for循环获取文档里面的数据
# # for line in f:
# #     print(line)  #每次返回一行的数据
# #2.读取文件所有的数据
# # print(f.read())
# # #3.读取文件并返回一个列表
# # print(f.readlines())
# print(f.readline())
# print(f.readline())
# print(f.readline())


# f.close()


# f = open('demo.txt',mode="w",encoding="utf-8")
# f.write('写点文章在里面吧')
# f.flush()
# f.close()

# fr = open('11.txt',mode="r",encoding="utf-8")
# fw = open('demo1.txt',mode="a",encoding="utf-8")
# for line in fr:
#     line = line.strip()
#     line = line.split(' ')
#     print(line)
#     age = line[2]
#     if int(age)>20:
#         fw.write(" ".join(line))
#         fw.write("\n")
# fr.close()
# fw.close()
# a=5
# try:
#     count= 1+a
# except Exception as e:
#     print(e)
# else:
#     print('没出现异常')
    

import time
from datetime import datetime,timedelta

# print(time.time()) #获取时间戳
# time.sleep(10)
# print('停顿五秒中去执行')

# nowTime = datetime.now() #2024-04-02 09:22:07.347163 得到一个时间类型的时间格式
# #转化为字符串格式的类型
# time_str = nowTime.strftime("%Y-%m-%d %H:%M:%S")
# print(time_str)


# print(time.time())
# print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))


# file_path =os.path.join('db','test','111.txt')
# print(file_path)

#绝对路径和相对路径
# print(__file__)
# print(os.path.abspath(__file__)) #获取文件所在的的绝对路径
# print(os.path.basename(os.path.dirname(__file__))) 

# print(__file__)
# print(os.path.abspath(__file__))
# print(os.path.dirname(os.path.abspath(__file__)))
# abs_dir = os.path.dirname(os.path.abspath(__file__)) #获取文件所在文件夹的绝对路径
# print(abs_dir)
# print(os.path.join(abs_dir,'demo1.txt')) #正确获取文件路径的方法


# name=input('请输入名字:')
# age=input("请输入年龄:")
# data_str = datetime.now().strftime("%Y-%m-%d %H:%M:%s")
# line = f"名字是:{name},年龄:{age},注册的时间是:{data_str}"
# dir_base = os.path.dirname(os.path.abspath(__file__))
# file_path = os.path.join(dir_base,'demo.txt')
# f = open(file_path,mode='w',encoding="utf-8")
# f.write(line)
# f.close()


# # 当前的时间加50天,20小时的具体时间 可以时间的加减
# now_date =datetime.now()
# late_time = now_date+timedelta(days=100,hours=20)
# print(late_time)

# time = timedelta(hours=9,minutes=13)  #指的一个时间对格式的9:13分


# #字符串类型的时间 转化为dateTime 类型的时间
# time_str = "2024-03-12 14:30:20" #字符串格式的时间
# fm_time =datetime.strptime(time_str,'%Y-%m-%d %H:%M:%S') #转化为时间类型的格式
# print(fm_time + timedelta(days=5)) #时间增加五天

# s_time ="06:40"
# e_time="17:30"

# fs = datetime.strptime(s_time,'%H:%M')
# fe = datetime.strptime(e_time,'%H:%M')
# pritn(fs-fe)


# text ="这个是一句话"
# bstr = text.encode(encoding="utf-8")
# print(bstr.decode('utf-8'))

# import requests
# res = requests.get(url="https://www3.autoimg.cn/newsdfs/g31/M08/C2/39/400x300_0_autohomecar__ChtlyGYL2FeAAFVTAACFYEFL_gY935.jpg")
# f =open('aa.jpg',mode="wb")
# f.write(res.content)
# f.close()

#with 的作用 open打开文件失败 也能自动关闭文件
# with open('11.txt',mode="r") as f:
#     data = f.read()
#     print(data)

#大文件读取的方式  一行一行的往下读

# f = open('demo44.txt',mode="r",encoding="utf-8")
# for line in f:
#     print(line.strip())
# f.close()


#excel的操作 load_workbook


wb = load_workbook('db/news.xlsx')
# v1 = wb.sheetnames #['北京','上海']
# sheet = v1['北京']
# print(sheet.cell(1,1).value)

##获取一个单元格的方式有两种 sheet.cell(1,1) sheet['B5']
# v2 = wb.worksheets
# sheet = v2[0]
# cell = sheet.cell(1,1)
# print(cell.value)

#获取单元格的某一行的方法 sheet[1] 读取第一行的所有单元格的列表
# v2 = wb.worksheets
# sheet = v2[0]
# cell_list =sheet[1]
# for cell in cell_list:
#     print(cell.value)

#获取所有行 sheet.rows

#读取某些行 sheet.iter_rows(min_row=2) 从第二行开始读取
#读取某些行 sheet.iter_rows(min_row=2,max_row=10) 从第二行开始读取,读到第十行

sheets =wb.worksheets;
sheet = sheets[0]
#获取单元格制定的位置
# cell = sheet.cell(1,1).value
# print(cell)
# print(sheet['A1'].value)
#获取一行单元格
# for i in sheet[1]:
#     print(i.value)
#获取全部的单元格
# rows = sheet.rows
# for row in rows:
#     for cell in row:
#         print(cell.value)

# for row in sheet.iter_rows(min_row=2,max_row=5):
#     print(row[1].value)

str = "200000分钟"
# print(str[:-2])

print(str[-4:-1])
















































  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_45925246

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值