Python: Parse one string into multiple variables?

2024/7/6 22:47:21

I am pretty sure that there is a function for this, but I been searching for a while, so decided to simply ask SO instead.

I am writing a Python script that parses and analyzes text messages from an input file. Each line looks like this:

Oct 24, 2014, 19:20 - Lee White: Hello world!

or:

Apr 4, 19:20 - Lee White: Hello world!

If the year in the datetime is not mentioned, it means that the message was sent in the current year.

What I want to do, is parse this string into multiple variables. Ideally, I am looking for a function that takes an input string, a format string, and a couple of variables to store the output in:

foo(input, "MMM DD, YYYY, HH:MM - Sender: Text", &mon, &day, &year, &hour, &minutes, &sender, &text)

Does such a thing exist in Python?

Answer

This uses the remarkably useful dateutil library to make date parsing easier - you can pip install python-dateutil or easy_install python-dateutil it. Split the data on the : and the - to get message and sender, then process the date text to get a datetime object where you can access its various attributes to get the components required, eg:

from dateutil.parser import parses = 'Apr 4, 19:20 - Lee White: Hello world!'
fst, _, msg = s.rpartition(': ')
date, _, name = fst.partition(' - ')
date = parse(date)
name, msg, date.year, date.month, date.day, date.hour, date.minute
# ('Lee White', 'Hello world!', 2015, 4, 4, 19, 20)
https://en.xdnf.cn/q/119577.html

Related Q&A

How do I pull multiple values from html page using python?

Im performing some data analysis for my own knowledge from nhl spread/betting odds information. Im able to pull some information, but Not the entire data set. I want to pull the list of games and the a…

Creating h5 file for storing a dataset to train super resolution GAN

I am trying to create a h5 file for storing a dataset for training a super resolution GAN. Where each training pair would be a Low resolution and a High resolution image. The dataset will contain the d…

How to resolve wide_to_long error in pandas

I have following dataframeAnd I want to convert it into the following format:-To do so I have used the following code snippet:-df = pd.wide_to_long(df, stubnames=[manufacturing_unit_,outlet_,inventory,…

Odoo 10: enter value in Many2one field dynamically

I added in my models.py :commercial_group = fields.Many2one("simcard.simcard")and in my views.xml :<field name="commercial_group" widget="selection"/>And then i am t…

How to erode this thresholded image using OpenCV

I am trying to first remove the captcha numbers by thresholding and then eroding it ,to get slim continuous lines to get better output. Problem:the eroded image is not continuous as u can see Original …

Searching for only the first value in an array in a csv file

So i am creating a account login system which searches a database for a username (and its relevant password) and, if found, will log the user on.This is what the csv file currently looks like[dom, ente…

how to write a single row cell by cell and fill it in csv file

I have a CSV file that only has column headers:cat mycsv.csvcol_1@@@col_2@@@col_3@@@col_3I have to fill a single row with None values in each cell of the CSV file. Can someone suggest me the best-optim…

Greedy String Tiling in Python

I am trying to learn greedy string tiling in algorithmI have two lists as follows:a=[a,b,c,d,e,f] b=[d,e,a,b,c,f]i would like to retrieve c=[a,b,c,d,e]Another example would be a = [1,2,3,4,5,6,7,8,9,1,…

Python - efficient way to create 20 variables?

I need to create 20 variables in Python. That variables are all needed, they should initially be empty strings and the empty strings will later be replaced with other strings. I cann not create the var…

Whatsapp asking for updating chrome version

I am trying to open whatsapp with selenium and python, it was working fine until today. In headless or non, whatsapp is now asking to update chrome, when I try to do so, Chrome throws this error: An er…