[英]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:为了检查自己,我尝试了这个:
Content of file:文件内容:
max_p_range_day = '2018-03-23'
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.