• Register
0 votes
39 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

1 vote
1 answer 17 views
17 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 13 views
13 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
1 vote
1 answer 18 views
18 views
I am new to this. I just want to know how to do that?
asked Nov 3, 2020 TeamScript 9.9k points
1 vote
1 answer 38 views
38 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
0 votes
1 answer 8 views
8 views
I am new to this. I was doing that but I got into some problems. I was working on that but I got into some problems.
asked Dec 3, 2020 TeamScript 9.9k points
0 votes
1 answer 19 views
19 views
I was working on that but I got into some problems.
asked Nov 3, 2020 TeamScript 9.9k points
0 votes
1 answer 36 views
36 views
I'm working on a code where I will input date in DD/MM/YYYY format for example 12/04/2020. And the output should show me the date like a weekday and March 12 2020 or something like that
asked Nov 3, 2020 TeamScript 9.9k points
0 votes
0 answers 16 views
16 views
Problem: I want the date to be displayed in the date month year format on my page using HTML only.
asked Oct 13, 2020 Ahsan 1.8k points
0 votes
1 answer 6 views
6 views
Problem: Write a Python program to get next day of a given date.Date is provided as day, month and year The input provided is always valid. Output should be day-month-year. Hint: print(day,"-",month,"-",year) will display day-month-yearAlso identify the test data and use it to test the program.
asked 1 day ago Mashhoodch 5.7k points
1 vote
1 answer 20 views
20 views
Problem: I’ve installed PyCharm right now to dig into the python’s world. I am trying to create a new project on it and it shows a dialogue “No python interpreter configured for the project” What is the way to get rid of it? Any clues?
asked Mar 6, 2020 Gavin 15.3k points