简体   繁体   中英

Transforming Complex Flat File using Python

I have a flat file which has format something like below,

Country{Year{Working_Days_Month1{Working_Days_Month2...{Working_Days_Month12
IND{2019{111110011111001111100111110011{111110011111001111100111110011....{111110011111001111100111110011

I need to transform the above data to a table or json as below with following columns,

Column 1 - Country

Column 2 - Year

Column 3 - Month{1-12)

Column 4 - Days (1-31)

Column 5 - Working Days of Particular Month ( 1 or 0 based on number of days present in flat file)

You can try to use read_csv from pandas.

import pandas as pd

df = pd.read_csv("/path/to/file", sep="{")

You can use names keyword argument in read_csv to give the column names as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM