微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用带有正则表达式的 python datetime 将儒略日期转换为 YYYYMMDD

如何解决使用带有正则表达式的 python datetime 将儒略日期转换为 YYYYMMDD

有没有办法使用带有正则表达式的python datetime将儒略日期转换为具有以下格式的日历:YYYYMMDD

我的字符串是这样的

abc_8g_1980_312.tif

其中 1980_312 分别是 YYYY_DDD。所需的输出应为 19801107 我正在尝试使用网站 https://regex101.com/

谢谢

解决方法

在转换日期时,正则表达式是错误的工作工具,您仍然可以使用它来提取字符串的“有趣”部分(来自图像名称的 182 git init 183 git add . 184 git commit -m "initial relese 3*3" 185 git branch -M main 186 git remote add origin https://github.com/sainivinit/TicTacToe.git 187 git push -u origin main 188 git push -u origin main -force 189 git push -u origin main --force 190 exit 191 git init 192 git add -A 193 git commit -m "working 10*10 or dynamic play board and shift hard to soft code" 194 git remote rm origin 195 git remote add origin https://github.com/sainivinit/TicTacToe.git 196 git push -u origin main 197 git init 198 git add -A 199 git commit -m "10*10 board solved some issue" 200 git remote rm origin 201 git remote add origin https://github.com/sainivinit/TicTacToe.git 202 git push -u origin main 203 exit 204 git init 205 git add . 206 git commit -m "initial realese" 207 git branch -M main 208 git remote add origin https://github.com/sainivinit/ShakshiTradres.git 209 git push -u origin main 210 git init 211 git add -A 212 git commit "10*10 board now playboard and players datasource is different" 213 git commit -m "10*10 board now playboard and players datasource is different" 214 git remote rm origin 215 git remote add origin https:///github.com/sainivinit/TicTacToe.git 216 git push -u origin main 217 git init 218 git add -A 219 git commit -m "10*10 board checkDiagonalsForWin" 220 git remote rm origin 221 git remote add origin https://github.com/sainivinit/TicTacToe.git 222 git push -u origin main 223 git init 224 git add -A 225 git commit -m "10*10 board now players will save in h2 and palyboard will save into mysql" 226 git remote rm origin 227 git remote add origin https://github.com/sainivinit/TicTacToe 228 git push -u origin main 229 exit 230 git init 231 git add -A 232 git commit -m "remove lots of bugs and use recomended pettern" 233 git remote rm origin 234 git remote add origin 235 git remote add origin https://github.com/sainivinit/TicTacToe.git 236 git push -u main 237 git push -u main --force 238 git remote add origin https://github.com/sainivinit/TicTacToe.git 239 git push -u origin main 240 git push -u origin main --force 241 exit 242 git init 243 git add -A 244 git commit -m "impletemns integration testing and solved some issue" 245 git remote rm origin 246 git remote add origin https://github.com/sainivinit/TicTacToe.git 247 git push -u origin main 248 git push -u origin main 249 git init 250 git add -A 251 git commit -m "complete game score" 252 git remote rm origin 253 git remote add origin https://github.com/sainivinit/TicTacToe.git 254 git push -u origin main 255 git clone https://github.com/sainivinit/TicTacToe.git 256 v 257 git init 258 git add -A 259 git commit "solved some issue" 260 git commit -m "solved some issue" 261 git remote rm origin 262 git remote add origin https://github.com/sainivinit/TicTacToe.git 263 git push -u origin main 264 exit 265 git init 266 git add -A 267 git commit -m "integration testing" 268 git remote rm origin 269 git remote add origin https://github.com/sainivinit/TicTacToe.git 270 git push -u main 271 git remote add origin https://github.com/sainivinit/TicTacToe.git 272 git push -u main 273 git remote add https://github.com/sainivinit/TicTacToe.git 274 git remote add origin https://github.com/sainivinit/TicTacToe.git 275 git push -u main 276 git push -u main --force 277 git init 278 git add -A 279 git commit -m "integration testing" 280 git remote rm origin 281 git remote add https://github.com/sainivinit/TicTacToe.git 282 git remote add origin https://github.com/sainivinit/TicTacToe.git 283 git push -u origin main 284 git push -u origin main 285 git init 286 git add -A 287 git commit -m "integration testing & solve some issue" 288 git remote rm origin 289 git remote add origin https://github.com/sainivinit/TicTacToe.git 290 git push -u origin main 291 git init 292 git add -A 293 git commit -m "integration testing" 294 git remote rm origin 295 git remote rm origin 296 git remote add origin https://github.com/sainivinit/TicTacToe 297 git push -u origin main 298 git remote add origin https://github.com/sainivinit/TicTacToe.git 299 git push -u origin main 300 git push -u origin main --force 301 git push -u origin master 302 git show-ref 303 git push origin HEAD:main 304 git show-ref 305 git push -u origin main 306 git push origin HEAD:main 307 git init 308 git add . 309 git commit -m "integration testing" 310 git push -u origin master 311 git push -u origin master --force 312 git push -u origin master --force 313 git init 314 git add -A 315 git commit -m "test integration" 316 git push origin HEAD:main 317 git push origin HEAD:main --force 318 git commit -m "test integration" 319 git reflog 320 git reflog 321 history ),但是:

YYYY_DDD

当然,如果可能出现更多的模式,我建议让模式更严格,例如你可以将上面的正则表达式放在一个组中,然后在它后面匹配 import re filename = 'abc_8g_1980_312.tif' date_str = re.search(r'\d{4}_\d+',filename).group() # '1980_312' 以确保它始终搜索 \.tif$ 文件名末尾的模式。如果它总是这种格式,您可以完全放弃正则表达式,只需拆分/分区您的字符串以提取“重要”部分,但我会将其作为练习留给您;)

从文件名中获得 tif 部分后,您可以使用 datetime.datetime.strptime() 获取实际日期,例如:

YYYY_DDD

最后,要获得所需的输出,您可以使用 datetime.datetime.strftime()

import datetime

real_date = datetime.datetime.strptime(date_str,'%Y_%j')
# datetime.datetime(1980,11,7,0)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。