简体   繁体   English

使用 Python 转换复杂平面文件

[英]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,我需要将上述数据转换为表格或 json,如下所示,

Column 1 - Country第 1 列 - 国家

Column 2 - Year第 2 栏 - 年份

Column 3 - Month{1-12)第 3 列 - 月{1-12)

Column 4 - Days (1-31)第 4 栏 - 天数 (1-31)

Column 5 - Working Days of Particular Month ( 1 or 0 based on number of days present in flat file)第 5 列 - 特定月份的工作日(1 或 0 基于平面文件中的天数)

You can try to use read_csv from pandas.您可以尝试使用read_csv中的 read_csv。

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.您也可以在read_csv中使用names关键字参数来指定列名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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