15,351,508 members
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

## Solution 1

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[^].

## Solution 2

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
```