alexa

Python Regex expression within a string in python ?

Python Regex expression within a string in python ?

If you want to turn your string into a clean pandas data frame, you could do something like this (assuming your input is well-formatted, i.e. '"name":"name_value", "ID":"ID_value", ...', no extra " or :):

 import re
import pandas as pd


inp = '"name":"fffff fff", "ID":"1234", "name":"matt lam","ID":"1255"'

inp = inp.replace('"', '') # remove all double quotes

inp_list = re.split(', |:|,', inp) # split on comma, comma + whitespace, colon and put elements in list

name_lst = inp_list[1::4] # get every 4th element, starting from second (all name values)
ID_lst = inp_list[3::4] # get every 4th elements, starting from fourth (all ID values)

d = {'name':name_lst,'ID':ID_lst} # Combine lists into dict

df = pd.DataFrame(d) # Turn dict into dataframe

df

192 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online

Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry