Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

How do I extract certain words with a specific key letters inside a file with in python ?

How do I extract certain words with a specific key letters inside a file with in python ?

Using re.findall we can try:

 # use this to read all lines into a string
with open('./models/asm/Draft_km.modelspec', 'r') as file:
    inp = file.read()

# otherwise we can hard code the data you showed in your question here
inp = """m_BSORx_kcat : 10
m_ENTERH_kcat : 10
m_TRPTRS_kcat : 10
m_EX_remnant1_e_kcat : 10
m_SCYSSL_kcat : 10
m_RNMK_kcat : 10
m_TAGtex_kcat : 10
m_URIDK2r_kcat : 10
m_TRPt2rpp_kcat : 10
m_GLUSy_kcat : 10
m_VPAMTr_copy2_kcat : 10
m_EX_galctn__L_e_km : 0.001
m_EX_galt_e_km : 0.001
m_EX_dgmp_e_km : 0.001
m_EX_galur_e_km : 0.001
m_EX_gam_e_km : 0.001
m_EX_gam6p_e_km : 0.001
m_EX_gbbtn_e_km : 0.001"""

matches = re.findall(r'\b\w+_kcat : \d+(?:\.\d+)?', inp)
output = ', '.join(matches)
print(output) 

This prints:

m_BSORx_kcat : 10, m_ENTERH_kcat : 10, m_TRPTRS_kcat : 10, m_EX_remnant1_e_kcat : 10, m_SCYSSL_kcat : 10, m_RNMK_kcat : 10, m_TAGtex_kcat : 10, m_URIDK2r_kcat : 10, m_TRPt2rpp_kcat : 10, m_GLUSy_kcat : 10, m_VPAMTr_copy2_kcat : 10


7 0

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