0

Due to the fact that now Pandas doesn't have the to_fwt function, I've found a solution that should solve this problem:

from tabulate import tabulate

def to_fwf(df, fname):
    content = tabulate(df.values.tolist(), list(df.columns), tablefmt="plain")
    open(fname, "w").write(content)

pd.DataFrame.to_fwf = to_fwf

But it doesn't write a file in the fixed-width format as it was declared. It seems to me that I should pass the colspecs parameter somehow. Any ideas here?

Valentyn
  • 562
  • 1
  • 7
  • 21
  • what's wrong with df.to_string(filepath, index=False) ? – Dariusz Krynicki Sep 28 '21 at 11:29
  • @darkman It doesn't write in the fixed-width format – Valentyn Sep 28 '21 at 11:32
  • It seems to be the solution of another question https://stackoverflow.com/questions/16490261/python-pandas-write-dataframe-to-fixed-width-file-to-fwf − so it seems to work. If you want help you need to be more specific: what do you expect to get, and what is different? Please show data, e.g. file contents in a code block – Cimbali Sep 28 '21 at 12:42
  • IMHO the `numpy.savetxt()` solution in that question solves the issue better than this tabulate solution. – Jaime M. Feb 23 '23 at 12:00

0 Answers0