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.
I can do this with a 'for' or 'while'? because the file has many lines, many lines...
– Celso Lopes
:-) 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)
– jsbueno
Sure @Celsolopes, I’ll put an example with your Cod
– Miguel
@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
@Miguel, I tested and it worked, but when I do in the file is not working, see my code and error:
– Celso Lopes
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)
– Celso Lopes
@Celsolopes: https://repl.it/repls/BlandRichPiranha
– Miguel
@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?
– Celso Lopes
@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!!!
– Celso Lopes
@Celsolopes still good. Good luck
– Miguel
Thanks to your help ;)
– Celso Lopes