Reputation: 1
New learner here. I'm trying to write a fixed-width file in Python. I did a lot of google search to find a module the can help achieve the goal. I'm attempting with numpy
. The function is not respecting any of the provided delimiters.
import pandas as pd
import numpy as np
data = {
"Make": ['BMW', 'Toyota', 'Dodge', 'Honda'],
"Model": ['X5', 'Camry', 'Caravan', 'Accord'],
"Weight": [1020.25, 950.98, 1235.37, 940.9],
"Year": [2021, 2023, 2019, 2014]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)
print(df)
with open('test_output.txt', 'w') as outfile:
fmt = '%-6s %-7s %-7s %-4s'
np.savetxt(outfile, df.values,delimiter='*', fmt=fmt) #Testing with any delimiter
My desired output should not contain any spaces between the columns. I attempted delimiter=''
and delimiter=' '
. The function is not respecting any delimiter when I provide an fmt
.
I'm open to use different modules.
Upvotes: 0
Views: 59