You can use read_csv
with create groups by compare first column by product
string and pivot
:
df = pd.read_csv('file.txt', header=None, sep=': ', engine='python')
df = df.assign(g = df[0].eq('product').cumsum()).pivot('g',0,1)
print (df)
0 description product rating review
g
1 product 1 desc 1 7.8 product 1 review
2 product 2 desc 2 4.5 product 2 review
3 product 3 desc 3 8.5 product 3 review
Or create list of dictionaries:
#https://stackoverflow.com/a/18970794/2901002
data = []
current = {}
with open('file.txt') as f:
for line in f:
pair = line.split(':', 1)
if len(pair) == 2:
if pair[0] == 'product' and current:
# start of a new block
data.append(current)
current = {}
current[pair[0]] = pair[1].strip()
if current:
data.append(current)
df = pd.DataFrame(data)
print (df)
product description rating review
0 1 product 1 desc 7.8 product 1 review
1 2 product 2 desc 4.5 product 2 review
2 3 product 3 desc 8.5 product 3 review
Or reshape each 4 values to 2d numpy array and pass to DataFrame
constructor:
df = pd.read_csv('file.txt', header=None, sep=': ', engine='python')
df = pd.DataFrame(df[1].to_numpy().reshape(-1, 4), columns=df[0].iloc[:4].tolist())
print (df)
product description rating review
0 1 product 1 desc 7.8 product 1 review
1 2 product 2 desc 4.5 product 2 review
2 3 product 3 desc 8.5 product 3 review