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

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)
4 1
1,480 points

Related questions

1 vote
1 answer 15 views
15 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 LizzyM 6.1k points
1 vote
1 answer 12 views
12 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 samhaz 5k points
1 vote
1 answer 36 views
36 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 samhaz 5k points
0 votes
0 answers 5 views
5 views
Problem: I want the date to be displayed in the date month year format on my page using HTML only.
asked Oct 13 Ahsan 1.8k points
0 votes
0 answers 19 views
19 views
I have a text file that consists of similar lines and few are half similar to other lines in a text file. Input.txt I would like to play: Volleyball I would like to play: Volleyball I would like to play: TableTennis I would like to play: Baseball I do not know how ... collected[key]}\n') I can print only the second line. Don't know where I am going wrong. Any help would be appreciated. Thank you
asked Jul 16 anonymous