• Register
0 votes
65 views
My programme:

import pandas as pd

import re

 

df = pd.read_csv('path of csv file')

corpus = []

for i in range(0,df.shape[0]):

    x = df.iloc[i]['OrderTime']

    if re.search(r'\d{2}/\d{2}/\d{4}',x):  ### for mm/dd/yyyy

        y= re.findall(r'\d{2}/\d{2}/\d{4}',x)

        corpus.append(y)

    elif re.search(r'\d{1}/\d{1}/\d{4}',x): ### for m/d/yyyy

        y= re.findall(r'\d{1}/\d{1}/\d{4}',x)

        corpus.append(y)

    elif re.search(r'\d{1}/\d{2}\d{4}',x): ### for m/dd/yyyy

        y= re.findall(r'\d{1}/\d{2}/\d{4}',x)

        corpus.append(y)

    elif re.search(r'\d{2}/\d{1}\d{4}',x): ### for mm/d/yyyy

        y= re.findall(r'\d{2}/\d{1}/\d{4}',x)

        corpus.append(y)

    else:                     ### empty cells or says nan

        y=["00/00/0000"]

        corpus.append(y)

 

print(corpus)

My programme consits of coloumn that consits of string that contains date and time but I am not getting the actual result. I need the regex for m/dd/yyyy and mm/d/yyyy.
6 2
830 points

Please log in or register to answer this question.

1 Answer

0 votes
You can get the exact result by cahnging the orders of the regex and put the specific ones first.Your regex is not using any anchors thats why it may show m/d/yyyy in place of mm/d/yyyy.

You can also write \d{1} as \d and your code should be like :

import pandas as pd

import re

 

df = pd.read_csv('path of csv file')

corpus = []

for i in range(0,df.shape[0]):

    x = df.iloc[i]['OrderTime']

    if re.search(r'\d{2}/\d{2}/\d{4}',x):  ### for mm/dd/yyyy

        y= re.findall(r'\d{2}/\d{2}/\d{4}',x)

        corpus.append(y)

    elif re.search(r'\d{2}/\d/\d{4}',x): ### for mm/d/yyyy

        y= re.findall(r'\d{2}/\d/\d{4}',x)

        corpus.append(y)

    elif re.search(r'\d/\d{2}/\d{4}',x): ### for m/dd/yyyy

        y= re.findall(r'\d/\d{2}/\d{4}',x)

        corpus.append(y)

    elif re.search(r'\d/\d/\d{4}',x): ### for m/d/yyyy

        y= re.findall(r'\d/\d/\d{4}',x)

        corpus.append(y)

    else:                     ### empty cells or says nan

        y=["00/00/0000"]

        corpus.append(y)

 

print(corpus)
5 2
1,480 points

Related questions

0 votes
1 answer 2 views
2 views
Problem: What is the MM/DD/YYYY regular expression and how do I use it in php?
asked Mar 30 ummesalma 25.2k points
1 vote
1 answer 28 views
28 views
Problem: I was trying to validate date and time using regex in python so I wrote this code: import pandas as pd import re df = pd.read_csv('path of csv file') coorpus = [] for i in range(0,df.shape[0]): x = df.iloc[i]['OrderTime'] if re.search(r'\d{2}/\d ... of string that contains date and time but I am not getting the actual result. I need the regex for m/dd/yyyy and mm/d/yyyy. Can anyone help?
asked Apr 5, 2020 LizzyM 6.1k points
1 vote
1 answer 19 views
19 views
Problem: I tried to get the output for the format date mm/dd/yyyy, So I wrote this program: import pandas as pd import re df = pd.read_csv('path of csv file') coorpus = [] for i in range(0,df.shape[0]): x = df.iloc[i]['OrderTime'] if re.search(r'\d{2}/\d ... append(y) else: y=["00/00/0000"] coorpus.append(y) print(coorpus) But I did not get the actual result. Can anyone help me to get this format?
asked May 12, 2020 samhaz 5k points
0 votes
1 answer 3 views
3 views
Problem: I'm having trouble finding a solution; could you please assist me “How change date format in excel from dd/mm/yyyy to mm/dd/yyyy”?
asked Mar 30 tuhin1 48.9k points
0 votes
1 answer 2 views
2 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 30 rakib1 51.5k points
1 vote
1 answer 47 views
47 views
I am new to this. I just want to know how to do that?
asked Nov 3, 2020 TeamScript 13.5k points
0 votes
1 answer 3 views
3 views
Problem: I need help in resolving this problem: “Convert dd/mm/yyyy to yyyy-mm-dd in sql”?
asked Mar 30 tuhin1 48.9k points
0 votes
1 answer 1 view
1 view
Problem: I have a date in the format YYYY-MM-DD HH:MM:SS. But I want to retrieve the current time in the format YYYY-MM-DD HH:MM:SS.MS. So how can I get milisecond?
asked 17 hours ago anika11 4.6k points
1 vote
1 answer 4 views
4 views
Problem: Please help me. How can I solve it? python string to date yyyy-mm-dd
asked Mar 19 MUHAMMAD MUNEEB 83k points
1 vote
1 answer 45 views
45 views
Problem: I wrote a program to check if string matches regex list where : list = ["all*", "in*", "fo.*", "en"] string = "allinformen" The code I wrote is: import re # initializing list my_list = ["all*", "in*", "fo.*", "en"] # printing list print(" ... codition else it throws an error message: File "main.py", line 16 if res = True : ^ SyntaxError: invalid syntax What is the problem with my code?
asked Apr 28, 2020 samhaz 5k points