Unix timestamp conversion

63 views Asked by At

I have a datetime in this format "frame.time": "Dec 7, 2023 06:55:20.320000000 Mitteleuropäische Zeit" in my json script and I want to extract this json script into .csv so I want to convert it into Unix timestamp using python because I am already using python for my task.

I used a Python script like,

JSON data:

{
    "_index": "packets-2023-12-07",
    "_type": "doc",
    "_score": null,
    "_source": {
      "layers": {
        "frame": {
          "frame.encap_type": "1",
          **"frame.time": "Dec  7, 2023 06:55:20.320000000 Mitteleuropäische Zeit",**
          "frame.offset_shift": "0.000000000",
          "frame.time_delta": "0.119000000",
          "frame.time_delta_displayed": "0.000000000",
          "frame.time_relative": "10.585000000",
          "frame.number": "86",
          "frame.len": "72",
          "frame.cap_len": "72",
          "frame.marked": "0",
          "frame.ignored": "0",
          "frame.protocols": "eth:ethertype:ipv6:udp:v2gtp"
        },

Python(solution):

import pandas as pd

frame_time  = ev['_source']['layers']['frame'].get('frame.time_epoch', {})

dt_object = pd.to_datetime(frame_time, format="%b %d, %Y %H:%M:%S.%f00000000 Mitteleurop\u00e4ische Zeit")

extracted_values.append({"[1] frame.number": frame_number,
                         "[2] frame.time": dt_object,
                                     .
                                     .
                                     .
                                     .
                                                         })

with open('extracted_values.json', 'w') as f:  
    json.dump(extracted_values,  f, indent=2, separators=(', \n', ': '))

***By using above python code I got an error as below***,

Traceback (most recent call last):
  File "c:\Users\dhaval_tarapra\Desktop\Code\Pythoncodes\warm20degree.py", line 15, in <module>
    frame_time_epoch = pd.to_datetime(frame_time, format='%b %d, %Y %H:%M:%S.%f utf-8').timestamp()
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dhaval_tarapra\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pandas\core\tools\datetimes.py", line 1146, in to_datetime
    result = convert_listlike(np.array([arg]), format)[0]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\dhaval_tarapra\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\pandas\core\tools\datetimes.py", line 488, in _convert_listlike_datetimes
    return _array_strptime_with_fallback(arg, name, utc, format, exact, errors)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 519, in _array_strptime_with_fallback
    result, timezones = array_strptime(arg, fmt, exact=exact, errors=errors, utc=utc)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "strptime.pyx", line 534, in pandas._libs.tslibs.strptime.array_strptime
  File "strptime.pyx", line 355, in pandas._libs.tslibs.strptime.array_strptime
ValueError: time data "Dec 16, 2011 18:14:19.394000000 Mitteleuropäische Zeit" doesn't match format "%b %d, %Y %H:%M:%S.%f utf-8", at position 0. You might want to try:
    - passing `format` if your strings have a consistent format;
    - passing `format='ISO8601'` if your strings are all ISO8601 but not necessarily in exactly the same format;
    - passing `format='mixed'`, and the format will be inferred for each element individually. You might want to use `dayfirst` alongside this.

Thank you in advance!

0

There are 0 answers