Add spaces to the end of the field - python

Asked

Viewed 1,254 times

1

Hello, everybody.

I have a *.txt file in which there is a column with the product code and another column with the quantity value as below:

Cód.          qtd
7513020087041;5.0

879705341017;24.0

11713777;8.0

17565097;2.0

181420;20.0

181421;20.0

But the system to which I export this file does not accept because the code field must be formatted with 13 characters, and if it does not have the 13 characters it must be filled with white spaces as in the example below:

7513200870410;5.0

879075341017_;24.0

11713777_____;8.0

17565097_____;2.0

181420_______;20.0

181421_______;20.0

Note: Read '_' = ' spaces.

1 answer

2


You can use ljust:

print('7513200870410'.ljust(13)) # '7513200870410'
print('181420'.ljust(13)) # '181420       '

Or format (python3.x):

print('{:<13}'.format(7513200870410)) # '7513200870410'
print('{:<13}'.format(181420)) # '181420       '

Example with your data for later saving to a file:

dados = '''
    7513020087041;5.0
    879705341017;24.0
    11713777;8.0
    17565097;2.0
    181420;20.0
    181421;20.0
'''

dados_new = ''
for d in dados.split():
    cod, qtd = d.split(';')
    dados_new += '{:<13};{}\n'.format(cod, qtd)

# guardar dados_new em um novo ficheiro:
""" OUTPUT
7513020087041;5.0
879705341017 ;24.0
11713777     ;8.0
17565097     ;2.0
181420       ;20.0
181421       ;20.0
"""

DEMONSTRATION

If you want to save to a file again with all formatted you can, after the code of the example above:

...
print('\n'.join('{};{}'.format(x,y) for x,y in dados_new), file=open('new_file.txt', 'w'))

# output:
"""
7513020087041;5.0
879705341017 ;24.0
11713777     ;8.0
17565097     ;2.0
181420       ;20.0
181421       ;20.0
"""

DEMONSTRATION

  • I can do this with a 'for' or 'while'? because the file has many lines, many lines...

  • 1

    :-) I was going to comment on the use of the format (and in Python 3.6, the fstrings also accept the parameters used by the format)

  • Sure @Celsolopes, I’ll put an example with your Cod

  • @jsbueno, I knew that format also gave but I had to test because I no longer remembered the syntax well. Obvious by the repair

  • @Miguel, I tested and it worked, but when I do in the file is not working, see my code and error:

  • with open('Products.txt', 'a') as Arq: arq_new = [] for d in Arq.split(): Cod, Qtd = d.split(';') arq_new.append('{:<13}'. format(Cod), Qtd)) print(arq_new)

  • @Celsolopes: https://repl.it/repls/BlandRichPiranha

  • @Miguel, that’s right, thank you very much, man. Now when I play for a new file it is creating a line jump ' n' leaving me with a blank line between a given and another, as I remove this?

  • @Miguel, I got it, it was worth it!! just remove ' n' from the last line where Oce gave me : print(' n'. Join('{};{}'. format(x,y) for..., so :print('. Join('{};{}'. format(x,y) for... Thank you very much indeed!!!

  • @Celsolopes still good. Good luck

  • 1

    Thanks to your help ;)

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.