日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python输入一个字符一个数字_Python:如何只对字符串中的数字字符加/减一个数字?...

發(fā)布時(shí)間:2023/12/10 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python输入一个字符一个数字_Python:如何只对字符串中的数字字符加/减一个数字?... 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

盡管注釋中不鼓勵(lì)使用regex,但可以使用regex將時(shí)間對(duì)象解析為datetime.time對(duì)象,對(duì)它們執(zhí)行必要的計(jì)算,然后以所需格式打印它們:# datetime module for time calculations

import datetime

# regex module

import re

# seconds to add to time

myinp = 4

# List of data strings

# data = 'John Time Made 11:05:20 in 2010', '5.001 Kelly', '6.005 Josh'

with open('data.txt') as f:

data = f.readlines()

new_data = []

#iterate through the list of data strings

for time in data:

try:

# First check for 'HH:MM:SS' time format in data string

# regex taken from this question: http://stackoverflow.com/questions/8318236/regex-pattern-for-hhmmss-time-string

match = re.findall("([0-1]?\d|2[0-3]):([0-5]?\d):([0-5]?\d)", time)

# this regex returns a list of tuples as strings "[('HH', 'MM', 'SS')]",

# which we join back together with ':' (colon) separators

t = ':'.join(match[0])

# create a Datetime object from indexing the first matched time in the list,

# taken from this answer http://stackoverflow.com/questions/100210/what-is-the-standard-way-to-add-n-seconds-to-datetime-time-in-python

# May create an IndexError exception, which we catch in the `except` clause below

orig = datetime.datetime(100,1,1,int(match[0][0]), int(match[0][1]), int(match[0][2]))

# Add the number of seconds to the Datetime object,

# taken from this answer: http://stackoverflow.com/questions/656297/python-time-timedelta-equivalent

newtime = (orig + datetime.timedelta(0, myinp)).time()

# replace the time in the original data string with the newtime and print

new_data.append(time.replace(t, str(newtime)))

# catch an IndexError Exception, which we look for float-formatted seconds only

except IndexError:

# look for float-formatted seconds (s.xxx)

# taken from this answer: http://stackoverflow.com/questions/4703390/how-to-extract-a-floating-number-from-a-string

match = re.findall("\d+\.\d+", time)

# create a Datetime object from indexing the first matched time in the list,

# specifying only seconds, and microseconds, which we convert to milliseconds (micro*1000)

orig = datetime.datetime(100,1,1,second=int(match[0].split('.')[0]),microsecond=int(match[0].split('.')[1])*1000)

# Subtract the seconds from the Datetime object, similiar to the time addtion in the `try` clause above

newtime = orig - datetime.timedelta(0, myinp)

# format the newtime as `seconds` concatenated with the milliseconds converted from microseconds

newtime_fmt = newtime.second + newtime.microsecond/1000000.

# Get the seconds value (first value(index 0)) from splitting the original string at the `space` between the `seconds` and `name` strings

t = time.split(' ')[0]

# replace the time in the original data string with the newtime and print

new_data.append(time.replace(t , str(newtime_fmt)))

with open('new_data.txt', 'w') as nf:

for newline in new_data:

nf.write(newline)

new_data.txt文件內(nèi)容應(yīng)為:

^{pr2}$

總結(jié)

以上是生活随笔為你收集整理的python输入一个字符一个数字_Python:如何只对字符串中的数字字符加/减一个数字?...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。