I have txt file which look like this:

```html
Quod equidem non reprehendo;
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quibus natura iure responderit non esse verum aliunde finem beate vivendi, a se principia rei gerendae peti; Quae enim adhuc protulisti, popularia sunt, ego autem a te elegantiora desidero. Duo Reges: constructio interrete. Tum Lucius: Mihi vero ista valde probata sunt, quod item fratri puto. Bestiarum vero nullum iudicium puto. Nihil enim iam habes, quod ad corpus referas; Deinde prima illa, quae in congressu solemus: Quid tu, inquit, huc? Et homini, qui ceteris animantibus plurimum praestat, praecipue a natura nihil datum esse dicemus?
=========================================================================
Planet   Number   festival   animal
                  colour     book
Mercury  First    firecrack  phone
Venus    Last     kite       computer
Earth    Country  rangoli    tv
Jupiter  C.COD     bomb       

---------------------------------------------------------------------
11      4526      diwali      dog
                  holi        bigb
12      Joe       diwali      111
45      Doe       sankaranti  acer
65      UK        diwali      pan
67      22        diwali      
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Planet   Number   festival   animal
                  colour     book
Mercury  First    firecrack  phone
Venus    Last     kite       computer
Earth    Country  rangoli    tv
Jupiter  C.COD     bomb     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
45       5637      ganesh    tiger
                   holi      cinema
67       micael    holi      222
78       john      diwali    xamoi
90       france    diwali    hp
34       34        diwali   
```

I want to convert this text file into csv format.
The output I would like to show:
output:[output](https://i.stack.imgur.com/7Lfdf.png)

My code:

```
from itertools import groupby, chain
 with open("file.txt", "r") as fin,\
      open("file.csv", "w") as fout:
for key, group in groupby(fin, key=lambda line: bool(line.strip())):
    if key:
        zipped = zip(*(line.rstrip().split() for line in group))
        fout.write(",".join(chain(*zipped)) + "\n")

```