I have text file with data that is separated by Indentation. I want to create a dataframe from the data. The indentation separates the Ids and then within each there are different sets and each set has their data where fields like "Main_id", "Secondary_id", "Type", Value", "Start_Date", "End Date" that will repeat for "Category" and "Capacity" (If present multiple times). The Id and set value will also repeat for the various rows. The code will take the text file as input and separate the values in the columns of a dataframe in output.
I need a loop but I can't figure out how to repeat for some data and not for others. Please any directions/suggestions?
with open('test.txt') as file:
data = file.read()
list_sep=data.split('\i')
list_1=list_sep[1].split('\n')
q_columns=["Id", "set","Main_id", "Secondary_id","Quantity", "Type",
"Value", "Category", "Capacity", "Start Date", "End Date"]
df_q=pd.DataFrame(columns=q_columns)
Sample text file:
Total ids entered : 3
Id : 6050
set count = 1
set : 256
Main_id : 5677002
Secondary_id : 34248
Quantity : 6
Type : AB12
Value : crypt
Category : DFR
Capacity : 100
Category: BAS
Capacity: 500
Start_Date : 04/01/2022
End_Date : 1/31/2023
Id : 123
set count = 2
set : 456
Main_id : 123456789
Secondary_id : 101112131
Quantity : 4
Type : AB12
Value : crypt
Category : DFR
Capacity : 100
Start_Date : 04/01/2022
End_Date : 1/31/2023
set : 789
Main_id : 675497072432
Secondary_id : 265902648927
Quantity : 1
Type : MAC12
Value : nocl
Category : DFR
Capacity : 200
Quantity : 2
Type : GC56
Value : crypt
Category : BAS
Capacity : 300
Start_Date : 01/01/2023
End_Date : 01/31/2023
Id : 897
set count = 1
set : 956
Main_id : gy4567890
Secondary_id : ky234248
Quantity : 6
Type : MAB13
Value : nocl
Category : gcl
Capacity : 100
Start_Date : 04/01/2022
End_Date : 1/31/2023
