Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Input in a text file :
Combination: 1 . 1 . 1 . 1
var_a: 33
op: -9.234727046103648
Combination: 1 . 1 . 1 . 2
var_a: 31
op: -9.234270216065962
Combination: 1 . 1 . 1 . 3
var_a: 39
op: -9.233204788566372
Combination: 1 . 1 . 1 . 4
var_a: 50
op: -9.235195862495413


Desired output in a csv/txt:
combination,var_a,op
1.1.1.1,33,-9.234727046103648
1.1.1.2,31,-9.234270216065962
1.1.1.3,39,-9.233204788566372
1.1.1.4,50,-9.235195862495413 <pre>

What I have tried:

Should I use regex or is there a simple way?
Posted
Updated 25-Apr-22 22:14pm

You should create a list of tuples from the inputs. Read each set of three lines, and extract the values. Create a tuple of these values and add it to the list. Once you have all the data you can write it out to a .csv file.

See 7. Input and Output — Python 3.10.4 documentation[^] and 5. Data Structures — Python 3.10.4 documentation[^].
 
Share this answer
 
There is a way simpler than using regular expressions: you can split every line, using ":" as separator, e.g.
Python
line = "Combination: 1 . 1 . 1 . 1"
l = line.split(":")
print(l[1])
outputs
1 . 1 . 1 . 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900