简体   繁体   English

如何将字符串类型的“dd/mm/yyyy”格式化为python中单引号括起来的“yyyy-mm-dd”?

[英]How to format “dd/mm/yyyy” of string type to “yyyy-mm-dd” enclosed in single quotes in python?

Problem Statement: assign the date (where there is maximum pressure difference) in string format as 'yyyy-mm-dd'.问题陈述:以字符串格式将日期(存在最大压差的地方)指定为“yyyy-mm-dd”。 Make sure you enclose it with single quote.确保用单引号括起来。

test.py file is provided with the question for checking my notebook. test.py 文件提供了检查我的笔记本的问题。 Its read only and I am not allowed to edit it.它是只读的,我不允许编辑它。

test.py测试.py

import re
from hashlib import md5
import nbformat
import pickle

def read_ipynb_file(file_path):
    with open(file_path) as file:
        out = str(nbformat.read(file_path, as_version=4))
    return out

path = 'question/EDA_question.ipynb'

out = read_ipynb_file(path)


max_p_range_day =  re.findall(r'max_p_range_day\s*=\s*\'\d*[-,]?\d*[-,]?\d*\'', out)[0].replace(' ', '').replace(' ', '').replace("'", "") 

Error:错误:

___________________________ ERROR collecting test.py ___________________________

test.py:23: in <module>

    max_p_range_day =  re.findall(r'max_p_range_day\s*=\s*\'\d*[-,]?\d*[-,]?\d*\'', out)[0].replace(' ', '').replace("'", "")

E   IndexError: list index out of range

In dataset "Day" column has dates which are type strings like this: "23/03/2018".在数据集“Day”列中的日期是这样的类型字符串:“23/03/2018”。

My code:我的代码:

....
df5=pd.to_datetime(df5['Day']).dt.date  # date which has maximum pressure diff   

from datetime import datetime
max_p_range_day = datetime.strftime(df5.values[0],"'%Y-%m-%d'")

This is printing the date in single quotes but shows error when test.py evaluates it.这是用单引号打印日期,但在 test.py 评估它时显示错误。

For checking myself, I tried this:为了检查自己,我尝试了这个:

  1. wrote max_p_range_day in file -在文件中写了 max_p_range_day -

Content of file:文件内容:

max_p_range_day = '2018-03-23'
  1. Then did this:然后这样做:

    x=open("handson_date.txt",'r') x=open("handson_date.txt",'r')

    out=x.read() out=x.read()

    res=re.findall(r'max_p_range_day\s*=\s*\'\d*[-,]?\d*[-,]?\d*'', out)[0].replace(' ', '').replace("'", "") res=re.findall(r'max_p_range_day\s*=\s*\'\d*[-,]?\d*[-,]?\d*'', out)[0].replace(' ' , '')。代替(”'”, ””)

There was no error.没有错误。 res showed 'max_p_range_day=2018-03-23'. res 显示“max_p_range_day=2018-03-23”。

But when my code is tested using test.py the error is thrown.但是当使用 test.py 测试我的代码时,会抛出错误。

Kindly suggest how to address this concern.请建议如何解决这个问题。

Change your code with below使用以下更改您的代码

re.findall(r"max_p_range_day\s*=\s*'\d*[-]\d*[-]\d*'",out)[0].replace(' ', '').replace("'", "")

This will work with you这将与您合作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中格式化单引号字符串“yyyy-mm-dd”中的日期? - How to format date in single quoted string “yyyy-mm-dd” in python? 在2014年1月4日给出时如何将mm / dd / yyyy格式转换为python 3.0中的yyyy-mm-dd - How to convert from mm/dd/yyyy format to yyyy-mm-dd in python 3.0 when given 1/4/2014 如何在 Python 中将日期格式 (YYYY-MM-DD) 转换为 (YYYY,MM,DD) - How to convert a date format (YYYY-MM-DD) to (YYYY,MM,DD) in Python 如何在 Python 中将 mysql 日期时间 yyyy-mm-dd 格式化为 dd-mm-yyyy? - How to format a mysql datetime yyyy-mm-dd to dd-mm-yyyy in Python? Python`YYYY-MM-DD` - Python `YYYY-MM-DD` 如何在 python 中以“yyyy-mm-dd”格式添加一天? - how to add one day in 'yyyy-mm-dd' format in python? 在python中将yyyy-dd-mm转换为yyyy-mm-dd - convert yyyy-dd-mm to yyyy-mm-dd in python 如何将不同格式的字符串(YYYY-MM-DD,DD-MM-YYYY)转换为熊猫中一种格式的日期对象? - How to convert string of different format(YYYY-MM-DD, DD-MM-YYYY) to date object of one format in pandas? 将python中索引的日期格式从yyyy-dd-mm更改为yyyy-mm-dd - Change date format for an index in python from yyyy-dd-mm to yyyy-mm-dd (Python) 将日期格式从 yyyy-mm-dd 更改为 yyyy/mm/dd - (Python) changing date format from from yyyy-mm-dd to yyyy/mm/dd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM