'ValueError: not enough values to unpack (expected 3, got 1) using pandas

I have this data in fig which I want to extract with columnwise arrangement in pandas dataframe:

I have this code which is working fine if I have only three columns but not working for more than three columns and showing error: ValueError: not enough values to unpack (expected 3, got 1) fig of particle.txt file

with open('particle.txt', 'r') as fin:
    f_data = fin.read()
    columns = ["TIMESTEP", "id", "mass", "y"]
    data = []
    previous_line = ""

for line in f_data.split("\n"):
    if columns[0] in previous_line and columns[1] not in line:
        data.append({"TIMESTEP": line})
    elif columns[1] in previous_line and columns[0] not in line:
        data[len(data)-1]["id"], data[len(data)-1]["mass"], data[len(data)-1] 
["y"] = line.split(" ")
    elif all(col not in line for col in columns):
        data.append({"TIMESTEP": data[len(data)-1]["TIMESTEP"]})
        data[len(data)-1]["id"], data[len(data)-1]["mass"], data[len(data)-1] 
["y"] = line.split(" ")

    previous_line = line

df_1 = pd.DataFrame(data)

`



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source