Confused_scientist
Confused_scientist

Reputation: 35

Replace Tab with variable spaces in python

I have a text file that needs to be read by an outside program that uses spaces to delineate between rows.

As of right now the program reads: Field 1 then 6 spaces, field 2 then 2 spaces, field 3 1 space, field 4 2 spaces and so on. enter image description here When I modified the doc in excel, it is now tab delineated between each field. enter image description here How would I write a python script to be able to replace this white space with the proper number of so that this program can read my file?

Upvotes: 0

Views: 178

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54698

You can use "{:10s}{:3s}{:4s}".format(*fields) to do this. You just have to know the field widths.

Upvotes: 1

Related Questions