Wrote a program in Python
. It file.txt
next integer from file.txt
, processes it according to the specified rules and displays the result on the screen - a pair of integers. The result of processing must be recorded in a file for further processing. Sorry, I'm not a programmer, help, please!
- 1. What do "integer variables" mean? For python, these words do not make sense. 2. You need to write integer values. If in text format, then print. - alexlz
- Specify the task. Which file: binary or text file? - skegg
- For Python 2.7, this only works for a variable of type str, and I have to write a couple of integers (int) at the end of each iteration, which is not obtained in the proposed code. Obviously, it is necessary to translate them into stock form. I tried to change the type manually: 12345 -> '12345 \ n', the record is obtained and the main thing is then the number is read from the file! What is the code for this operation? - Fomich
- What are you talking about? print >> f, 12345 Is this? - alexlz
- onealexlz, please explain what executes the code print >> f, 12345 ?? - Fomich
|
1 answer
file_1 = open("file.txt", "w") # Открываем файл "file.txt" для записи. "w" - переписать, "a" - дописать в конец. # Всегда используем слеши вперед, даже на виндовсе # Если такого файла нет и прав достаточно, то он создастся. Если нет - будет ошибка. # Проверить наличие файла можно функцией exists(path) из os.path file_1.write(str(51)) # Записываем сюда что-то, предварительно преобразовав в строку командой str(x) file_1.close() # Закрываем файл
An example for the second python, text files. Details in the official documentation . By the way, in python there are no variables of strictly integer type, python is a language with dynamic typing.
- 3Python is a language with dynamic but strong typing. And it has
int
type - skegg - Well, yes ... it is logical. I meant that in python there are no constructions of type int a = 5 ;, variables are converted automatically. Or, in the case of write, not automatically ... - Zelta
- Generally cut out this phrase, so people do not embarrass) - Zelta
- four@Zelta for nothing. It's just that not everyone remembers / understands that with dynamic typing, the type has values, not variables. - alexlz
- oneRun the code a = 5 print type (a) and see what is printed - skegg
|