If you already have a list of dicts with equal keys, you should be able to do just this:
pandas.__version__ ->> '1.1.5'
dctlst = [{"a": 1, "b":1}, {"a":2, "b":2}]
from pandas import DataFrame
df = DataFrame(dctlst)
df
a b
0 1 1
1 2 2
Otherwise, You can use json to make a list of dictionaries out of it.
But first you have to clean up the text a bit (after reading it):
with open(r"C:\Users\User\Desktop\toDF.txt", "r") as txt:
txt = txt.read()
txt = txt.replace("Columns: [", "").replace("etc.", "").replace("\n", "")
If you don't remove the extra opening bracket and the other stuff json won't load it.
Also, json wants double quotes so replace single quotes with doubles:
txt = txt.replace("'", '"')
txt
'[{"city": "Zurich, Switzerland", "cost": "135.74"}, {"city": "Basel,
Switzerland", "cost": "135.36"}, {"city": "Lausanne, Switzerland", "cost": "131.24"},
{"city": "Lugano, Switzerland", "cost": "130.32"}, {"city": "Geneva, Switzerland",
"cost": "130.14"}, {"city": "Bern, Switzerland", "cost": "125.86"}, {"city": "Tromso,
Norway", "cost": "114.81"}, {"city": "Stavanger, Norway", "cost": "108.38"} ]'
Now it looks like a proper list of dictionaries which can be converted by json.loads
from json import loads
from pandas import DataFrame
lst = loads(txt)
df = DataFrame(lst)
df
city cost
0 Zurich, Switzerland 135.74
1 Basel, Switzerland 135.36
2 Lausanne, Switzerland 131.24
3 Lugano, Switzerland 130.32
4 Geneva, Switzerland 130.14
5 Bern, Switzerland 125.86
6 Tromso, Norway 114.81
7 Stavanger, Norway 108.38
If you want the rows with cities to look prettier you can look into string operations:
pandas string operations
this would work, but obviously depends on what you want:
df["city"] = df["city"].astype("string").str.replace(" ","")
df
city cost
0 Zurich,Switzerland 135.74
1 Basel,Switzerland 135.36
2 Lausanne,Switzerland 131.24
3 Lugano,Switzerland 130.32
4 Geneva,Switzerland 130.14
5 Bern,Switzerland 125.86
6 Tromso,Norway 114.81
7 Stavanger,Norway 108.38
And this would make it even better:
df[["city", "country"]] = df["city"].str.split(",", expand= True)
df
city cost country
0 Zurich 135.74 Switzerland
1 Basel 135.36 Switzerland
2 Lausanne 131.24 Switzerland
3 Lugano 130.32 Switzerland
4 Geneva 130.14 Switzerland
5 Bern 125.86 Switzerland
6 Tromso 114.81 Norway
7 Stavanger 108.38 Norway