-1

I have a csv with earthquake data. I would like to change the date format to make it easier to read and make parsing faster. I ran into this problem without being able to find a solution that works.

Since there are so many data, I put the first lines of the csv:

time,latitude,longitude,depth,mag,magType,nst,gap,dmin,rms,net,id,updated,place,type,horizontalError,depthError,magError,magNst,status,locationSource,magSource
1905-04-04T11:00:32.680Z,40.365,19.213,15,5.38,mw,,,,,iscgemsup,iscgemsup610548598,2022-05-09T15:22:45.142Z,"22 km WNW of Orikum, Albania",earthquake,,3.6,0.2,,reviewed,iscgemsup,iscgemsup
1905-04-29T01:47:02.700Z,46.094,7.04,15,5.3,mw,,,,,iscgem,iscgem610326285,2022-04-25T20:36:20.261Z,"1 km NNW of Martigny-Combe, Switzerland",earthquake,,3.1,0.4,,reviewed,iscgem,iscgem
1905-06-01T04:42:11.110Z,42.092,19.248,15,6.55,mw,,,,,iscgem,iscgem610326286,2022-04-25T20:36:26.209Z,"9 km E of Stari Bar, Montenegro",earthquake,,5.8,0.2,,reviewed,iscgem,iscgem

I created the form and display table via flask using python. So I use pandas for data search.

I've created some rudimentary code to try and change the date format with no success

import re
import csv
import pandas as pd

myList = []
filename = 'query.csv'
with open (filename, 'r', encoding="utf-8") as file:
    myFile = csv.reader(file)
    for row in myFile:
        myList.append(row)

I also created a code to convert the date format with what I have in mind

pattern = r"(\b[0-9]{4})([-]{1})([0-9]{2})([-]{1})([0-9]{2})([T]{1})([0-9:]{8})([.0-9Z]{5})"
dataString = '1905-04-04T11:00:32.680Z'
test = re.search(pattern, dataString)
day = str(t.group(5))
month = str(t.group(3))
year = str(t.group(1))
hour = str(t.group(7))
if month == "01":
    month = "January"
elif month == "02":
    month = "February"
elif month == "03":
    month = "March"
elif month == "04":
    month = "April"
elif month == "05":
    month = "May"
elif month == "06":
    month = "June"
elif month == "07":
    month = "July"
elif month == "08":
    month = "August"
elif month == "09":
    month = "September"
elif month == "10":
    month = "October"
elif month == "11":
    month = "November"
elif month == "12":
    month = "December"
datetime = year + '/' + month + '/' + day + ' - ' + hour

Kindly can you help me with this code?

Thank you for the time you have dedicated to me

Blazer
  • 1
  • 2
  • small typo. test = re.search(pattern, dataString) would be t = re.search(pattern, dataString) . Excuse me – Blazer Aug 10 '23 at 11:37
  • 2
    Don't convert your date format manually, use `strftime` – mozway Aug 10 '23 at 11:40
  • try using pandas option `to_datetime()` which converts your timestamp into a datetime object. This makes it much easier to manipualte datetime and use methods like `strftime`. https://pandas.pydata.org/docs/reference/api/pandas.Series.dt.html – simons____ Aug 10 '23 at 11:51

0 Answers0