Skip to content

Instantly share code, notes, and snippets.

@duchenpaul
Created September 25, 2020 02:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duchenpaul/ba0f43b6d846b8b274d36e3e1aa8c8fc to your computer and use it in GitHub Desktop.
Save duchenpaul/ba0f43b6d846b8b274d36e3e1aa8c8fc to your computer and use it in GitHub Desktop.
Convert dataframe to fwf
import pandas as pd
import pickle
import numpy as np
from tabulate import tabulate
left_align_gen = lambda length, value: eval(r"'{:<<<length>>}'.format('''<<value>>'''[0:<<length>>])".replace('<<length>>', str(length)).replace('<<value>>', str(value)))
right_align_gen = lambda length, value: eval(r"'{:><<length>>}'.format('''<<value>>'''[0:<<length>>])".replace('<<length>>', str(length)).replace('<<value>>', str(value)))
# df = pd.read_pickle("dummy.pkl")
with open("df.pkl", 'rb') as f:
df = pickle.load(f)
# field width defines here, width of each field
widths=(22, 255, 14, 255, 14, 255, 255, 255, 255, 255, 255, 22, 255, 22, 255, 255, 255, 22, 14, 14, 255, 255, 255, 2, )
# format datetime
df['CREATED_DATE'] = df['CREATED_DATE'].apply(lambda x: x.to_pydatetime().strftime('%Y%m%d%H%M%S'))
df['LAST_MODIFIED_DATE'] = df['LAST_MODIFIED_DATE'].apply(lambda x: x.to_pydatetime().strftime('%Y%m%d%H%M%S'))
df['TERMS_ACCEPTED_DATE'] = df['TERMS_ACCEPTED_DATE'].apply(lambda x: x.to_pydatetime().strftime('%Y%m%d%H%M%S'))
df['PRIVACY_ACCEPTED_DATE'] = df['PRIVACY_ACCEPTED_DATE'].apply(lambda x: x.to_pydatetime().strftime('%Y%m%d%H%M%S'))
# print(type(df.iloc[0]['CREATED_DATE']))
# print(df.iloc[0])
record_line_list = []
# for row in df.iloc[:10].itertuples():
for row in [tuple(x) for x in df.to_records(index=False)]:
record_line_list.append("".join(left_align_gen(length, value) for length, value in zip(widths, row)))
with open('output.txt', 'w') as f:
f.write('\n'.join(record_line_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment