Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

delete text and all new line characters between 2 words in pyhton

I have the following text as given

\nOUTPUTFORMAT \n  
\'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat\'\nLOCATION\n  
\'hdfs://nameservice1/user/hive/warehouse/dev_cmt.db/badge\'\nTBLPROPERTIES (\n  
\'spark.sql.create.version\'=\'2.4.0-cdh6.3.2\', \n  
\'spark.sql.sources.schema.numPartCols\'=\'1\', \n  \'spark.sql.sources.schema.numParts\'=\'1\'

I want to delete everything from words LOCATION till beginning of TBLPROPERTIES.
I am trying to use regex, but I have been unsuccesful till now.

\nOUTPUTFORMAT \n  
\'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat\'\nTBLPROPERTIES (\n  
\'spark.sql.create.version\'=\'2.4.0-cdh6.3.2\', \n  
\'spark.sql.sources.schema.numPartCols\'=\'1\', \n  
\'spark.sql.sources.schema.numParts\'=\'1\'

Thanks in advance for your suggestions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

import re
text = "\nOUTPUTFORMAT \n\'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat\'\nLOCATION\n\'hdfs://nameservice1/user/hive/warehouse/dev_cmt.db/badge\'\nTBLPROPERTIES (\n\'spark.sql.create.version\'=\'2.4.0-cdh6.3.2\', \n\'spark.sql.sources.schema.numPartCols\'=\'1\', \n\'spark.sql.sources.schema.numParts\'=\'1\'"
text = re.sub(r'LOCATION.*TBLPROPERTIES', 'TBLPROPERTIES', text, flags=re.DOTALL)
print(text)

See if this works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading