Write in a file numbers separated by a certain character

Asked

Viewed 568 times

1

I have to make a program that writes on disk a file with ordered numbers increasing from 1 to 100. Each number shall be separated by ;. The file should be called "growing.txt".

I did so:

arquivo= open("Crescente.txt","w")
for numeros in range(1,101):
 arquivo.write(str(numeros))
 print(numeros, end=".")
arquivo.close()

It’s going wrong: in Python appears with dots between each other, already in the saved file appears without dots.

  • Yes, with arquivo.write you define what will be written in the file and you put only the numbers; already with the print you set what will be displayed in the terminal and you placed the numbers with a dot at the end. What you described matches the code you wrote. What was the expected result?

  • Igor, if one of the answers solved your problem, you can choose the one you solved best (only one of them) and accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. And when I get 15 points, you can also vote in all the answers you found useful.

4 answers

2

arquivo= open("Crescente.txt","w")
for numeros in range(1,101):
   arquivo.write(str(numeros) + ', ')#adiciona a vírgula
   print(numeros, end=".")
arquivo.close()

If the file is not created Python automatically creates one with the name passed in the assignment.

imagem do arquivo

2

The print, by default, writes to sys.stdout (which in turn will display the message in the terminal where you are running Python). That is, it does not write to the file (unless you indicate, for example: print(mensagem, file=arquivo) writes in the archive - whereas arquivo was obtained by open).

In your case, what is being written in the file is only what is passed to arquivo.write. And since you only passed the number and nothing else, this is what will be written in the file. No print you printed the number followed by a dot, but as already said, this is not being written in the file, but "on screen" (in the terminal where you run Python).

If you want to write all the numbers in the file, and between them you must have a ; then do so:

with open('Crescente.txt', 'w') as arquivo:
    arquivo.write(';'.join(str(n) for n in range(1, 101)))

The use of with is interesting because it already closes the file at the end of the run.

Then I use join to join the numbers, using the ; as the character that will be between them. This way, the contents of the file will be:

1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71;72;73;74;75;76;77;78;79;80;81;82;83;84;85;86;87;88;89;90;91;92;93;94;95;96;97;98;99;100

As it is an exercise, and these exercises usually have artificial limitations like "you can’t use [ready function of language]", may not be able to use join. In this case, you could make one loop simple:

with open('Crescente.txt', 'w') as arquivo:
    for n in range(1, 100): # vou até o penúltimo número
        arquivo.write(f'{n};')
    arquivo.write('100')

I used a f-string to print the number followed by ;. This feature is available from Python 3.6, but if you are using a previous version, you can switch to:

arquivo.write('{};'.format(n))

The detail is that in for I went from 1 to 99 (the range(1, 100) includes numbers 1 to 99). That is, the for writ 1;2;3;....99;.

If I went up to 100, one ; would be placed at the end, after the 100. To avoid this, I go up to 99 and at the end (outside the loop) write the 100 directly.


If you want to make the algorithm more "generic":

inicio = 1
fim = 100

# com join    
with open('Crescente.txt', 'w') as arquivo:
    arquivo.write(';'.join(str(n) for n in range(inicio, fim + 1)))

# com loop
with open('Crescente.txt', 'w') as arquivo:
    for n in range(inicio, fim): # vou até o penúltimo número
        arquivo.write(f'{n};')
    arquivo.write(str(fim))

1

arquivo.write('teste')

The code line above puts the string 'test' inside the archive.


arquivo.write('teste' + ', ')

The line above puts 'test, ' inside the file. (with comma and space at the end).

-1

I found an easier way to do that:

arquivoC=open("Crescent.txt","w")

for num in range(1,101):
arquivoC.write(str(num)+";"+" n")
print(str(num)+";")

arquivoC.close()

  • 1

    How so easier? This is not exactly the same code as the other answers?

  • But the result is exactly the same

  • 1

    It does not produce. Yours, being the same code as of that answer adds a line break that should not exist, because clearly the statement you put asks that the numbers be separated by semicolon, not with line breaks;

Browser other questions tagged

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