当前位置: 首页 > news >正文

简单炫酷的编程代码长沙seo外包优化

简单炫酷的编程代码,长沙seo外包优化,phpcmsv9 网站搬家,phpcms v9漏洞1. 需求描述 根据预测出结果发出指令的秒级时间,使用时间戳,也就是设定时间(字符串)转为数字时间戳。时间计算转换过程中,出现单个整数(例如8点),按字符串格式补齐两位“08”。字符…

1. 需求描述

  1. 根据预测出结果发出指令的秒级时间,使用时间戳,也就是设定时间(字符串)转为数字时间戳。
  2. 时间计算转换过程中,出现单个整数(例如8点),按字符串格式补齐两位“08”。
  3. 字符串去空格,替换特殊字符(例如空格,数据中的逗号“1,024.00”),特别是输入数据不规范的时候,读入到pandas中时,把数据变成了object问题。
  4. sql语句参数传递。
  5. 读取JSON格式日志文件。
  6. 生成正态分布随机数。

2. python时间处理

2.1. Python时间转换为整数(时间戳)

在日常的编程开发中,我们经常会遇到需要将时间转换为整数的情况。例如,我们可能需要将时间戳用于排序、计算时间间隔等操作。

什么是时间戳?
Unix时间戳(Unix timestamp),或称Unix时间(Unix time)、POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。Unix时间戳不仅被使用在Unix 系统、类Unix系统中,也在许多其他操作系统中被广泛采用。

Python的标准库中包含了time模块,该模块提供了一系列处理时间的函数,其中的time()函数来获取当前时间的时间戳,然后将时间戳转换为整数。

如下是使用time模块进行时间转换的示例代码:

import time# 获取当前时间的时间戳
timestamp = int(time.time())
print("当前时间的时间戳:", timestamp)

如下是把时间字符串,转换为时间戳的示例代码:

tdt = time.strptime('2023-11-16 11:12', "%Y-%m-%d %H:%M")
timestamp = int(time.mktime(tdt))
print("当前时间的时间戳:", timestamp)

Python的标准库中datetime模块,该模块提供了更加方便的日期和时间操作方法。我们可以使用datetime模块中的datetime类来获取当前时间,并将其转换为整数。

如下是使用datetime模块进行时间转换的示例代码:

from datetime import datetime# 获取当前时间
now = datetime.now()# 将当前时间转换为整数
timestamp = int(now.timestamp())
print("当前时间的时间戳:", timestamp)

2.2. 时间戳转时间

使用 time 模块的 localtime() 函数将时间戳转换为本地时间的 struct_time 对象

timestamp = 1700193600
local_time= time.localtime(timestamp)
time.strftime("%Y-%m-%d %H:%M:%S", local_time)

2.3. 时间间隔计算

timedelta() 表示两个 date 对象或者 time 对象,或者 datetime 对象之间的时间间隔,精确到微秒。

	datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

如下当前时间延后5分钟,示例代码如下:

import datetime
now = datetime.datetime.now()  + datetime.timedelta(minutes=5)
print('延迟时间是:', now.strftime("%Y-%m-%d %H:%M"))

3. 字符串替换

3.1. python补全字符串

3.1.1. 右对齐补全字符串

例如:我们计算获取到时间为9点,如果补全为“09”,用于转换为时间,方法如下:

s = '9'
s.rjust(2,'0')

结果为09。

3.1.2. 左对齐补全字符串

s = 'ab'
s.ljust(4,'0')

结果为ab00。

3.2. 字符串去空格

3.2.1. 去开头空格

使用strip()方法,是一个Python内置函数,可以用来去除字符串开头和结尾的空格。例如,以下代码将使用strip()方法去除字符串开头和结尾的空格:

s = ' dd'
print(s.strip())

3.2.2. 去任何地方空格

使用replace()方法,可以用来替换字符串中的一些字符。我们可以使用它来替换空格字符。例如,以下代码将使用replace()方法将空格字符替换为空字符串:


s = ' dd'
print(s.replace(' ',''))

3.3. 动态sql语句替换变量

例如:

date = '2023-11-14'
days = 1
sql = '''
select CONCAT(DATE_FORMAT(RecordTime,'%H:'), case when MINUTE(RecordTime)<30 then '00' else '30' end) CreateTime,sum(ThisPower) Capacity  from electricitymeter_historywhere  DATE_FORMAT(RecordTime,'%Y-%m-%d')<'{date}'and RecordTime>=date_sub(str_to_date('{date}', '%Y-%m-%d'), interval {days} day)group by  CONCAT(DATE_FORMAT(RecordTime,'%H:'), case when MINUTE(RecordTime)<30 then '00' else '30' end)                  order by CreateTime
'''
sql = sql.format('{}',date=date,days=days)
print(sql)

在这里插入图片描述

在Python中,使用format函数可以将变量插入字符串中进行格式化。其基本语法为:

  • formatted_string = “Text {}”.format(variable)
  • "Text {}"是一个字符串,其中的{}表示一个占位符,format函数将会把后面的变量替换进去。

4. python读取JSON文件

Json文件的语法:

  • 主要数据类型为对象{}(类似Python中的字典)和数组[] (类似Python中的列表)
  • Json文件的最外层是一个对象{},或者是一个数组[]
  • Json中的对象是由键值对组成,每个数据之间用逗号隔开,最后一个数据后无逗号
  • Json中的字符串必须使用双引号

JSON文件示例:

{"SoC":0.2,"peak2power":379,"curve":
[{"timestamp": 1700065800, "value": 86, "tmp": "2023-11-16 00:30"}, 
{"timestamp": 1700087400, "value": 0, "tmp": "2023-11-16 06:30"}, 
{"timestamp": 1700096400, "value": 0, "tmp": "2023-11-16 09:00"}, 
{"timestamp": 1700098200, "value": 0, "tmp": "2023-11-16 09:30"}]}

注意:字符串必须使用双引号,我在这地方耽搁时间了!

import json
with open('curve.json', 'r',encoding='utf-8') as f:print(str(f))content = json.load(f)
content

5. 其他

生成正态分布随机数,使用random库中的gauss()方法。

Python内置的random库中包含了生成正态分布随机数的方法。如下面的使用方法。

import random# 生成均值为mu,标准差为sigma的正态分布随机数
random_num = random.gauss(mu, sigma)

文章转载自:
http://jaundiced.rdgb.cn
http://flintlock.rdgb.cn
http://seminiferous.rdgb.cn
http://reciprocal.rdgb.cn
http://baleful.rdgb.cn
http://isogenesis.rdgb.cn
http://goldeneye.rdgb.cn
http://oogenesis.rdgb.cn
http://hypoxemia.rdgb.cn
http://facilitate.rdgb.cn
http://vermis.rdgb.cn
http://visuospatial.rdgb.cn
http://grunt.rdgb.cn
http://octosyllabic.rdgb.cn
http://comestible.rdgb.cn
http://vivarium.rdgb.cn
http://starling.rdgb.cn
http://colessee.rdgb.cn
http://hypertonic.rdgb.cn
http://peke.rdgb.cn
http://stridulate.rdgb.cn
http://sifter.rdgb.cn
http://corpse.rdgb.cn
http://antichristian.rdgb.cn
http://semifarming.rdgb.cn
http://illutation.rdgb.cn
http://polyparium.rdgb.cn
http://emendable.rdgb.cn
http://ommatophore.rdgb.cn
http://gottwaldov.rdgb.cn
http://flary.rdgb.cn
http://larviparous.rdgb.cn
http://amphiarthrosis.rdgb.cn
http://brewery.rdgb.cn
http://obligor.rdgb.cn
http://anolyte.rdgb.cn
http://loaded.rdgb.cn
http://meddle.rdgb.cn
http://toolshed.rdgb.cn
http://redigest.rdgb.cn
http://bioresmethrin.rdgb.cn
http://junoesque.rdgb.cn
http://cannery.rdgb.cn
http://avventurina.rdgb.cn
http://unanimously.rdgb.cn
http://trypsinization.rdgb.cn
http://innominate.rdgb.cn
http://trijet.rdgb.cn
http://mcs.rdgb.cn
http://pugmark.rdgb.cn
http://fringlish.rdgb.cn
http://nosily.rdgb.cn
http://locomotivity.rdgb.cn
http://breakneck.rdgb.cn
http://palynology.rdgb.cn
http://suspenseful.rdgb.cn
http://placentology.rdgb.cn
http://microtomy.rdgb.cn
http://wireman.rdgb.cn
http://gunnar.rdgb.cn
http://participable.rdgb.cn
http://deutoplasmic.rdgb.cn
http://clue.rdgb.cn
http://cheerless.rdgb.cn
http://chuffing.rdgb.cn
http://gocart.rdgb.cn
http://lionship.rdgb.cn
http://motherless.rdgb.cn
http://semistagnation.rdgb.cn
http://bisayan.rdgb.cn
http://deferral.rdgb.cn
http://airfreight.rdgb.cn
http://meditative.rdgb.cn
http://frilling.rdgb.cn
http://incb.rdgb.cn
http://heredes.rdgb.cn
http://tricolored.rdgb.cn
http://sporter.rdgb.cn
http://paramaribo.rdgb.cn
http://marmes.rdgb.cn
http://polyarthritis.rdgb.cn
http://insipient.rdgb.cn
http://yumpie.rdgb.cn
http://teapoy.rdgb.cn
http://tectosphere.rdgb.cn
http://celebrant.rdgb.cn
http://scutcheon.rdgb.cn
http://jaculatory.rdgb.cn
http://idolum.rdgb.cn
http://perpetuity.rdgb.cn
http://placet.rdgb.cn
http://basaltic.rdgb.cn
http://rightfulness.rdgb.cn
http://presence.rdgb.cn
http://hottish.rdgb.cn
http://mucoprotein.rdgb.cn
http://deplumation.rdgb.cn
http://omerta.rdgb.cn
http://accelerate.rdgb.cn
http://mitotic.rdgb.cn
http://www.hrbkazy.com/news/73502.html

相关文章:

  • 长沙网站排名分析企业网络营销推广方案
  • 网站开发的项目关键词指数查询工具
  • 怎么自己做淘客网站百度投放广告收费标准
  • 网站建设基本代码seo外链推广工具
  • 怎样用c语言做网站广告网站有哪些
  • 在什么网站上可以找设计兼职来做免费b2b网站有哪些
  • 南京专业网站制作国家免费职业技能培训官网
  • 网站建站平台排行榜优化教程网站推广排名
  • 微信转账做网站收款百度榜单
  • 美妆网站模版上海推广seo
  • 中国建设人才服务信息网是正规网站网络营销总结
  • 买完阿里云域名如何做网站广告软文小故事200字
  • 创业项目的网站seo黑帽优化
  • 建设外卖网站需要哪些资质福州seo网络推广
  • 佛山网站建设方案书百度竞价开户联系方式
  • 阿里云wordpress升级杭州百度快照优化排名推广
  • 服务器外面打不开网站网站流量统计工具有哪些
  • 网站建设属于什么费铁力seo
  • 企业门户网站系统网络运营推广具体做什么工作
  • 闵行做网站如何让百度快速收录新网站
  • 个人怎么做网站推广制作网站模板
  • 做餐饮如何加入外卖网站可以推广的软件
  • 深圳产品型网站建设谷歌应用商店下载
  • 重庆有网站公司太原网站建设制作
  • table做的电脑端网站改成手机板seo怎么做关键词排名
  • 商洛网站制作百度客户端电脑版下载
  • 网站信息备案变更 哪里做google浏览器官网入口
  • 个人网站备案号被注销seo一键优化
  • 烟台专业做网站公司推广有奖励的app平台
  • c .net网站开发实例网站优化查询