Given a text file containing integers. It is necessary to remove all even numbers from it. Tell me please.

The text in the file: "The definition of integers states that any of the numbers 1, 2, 3, 4, the number 0, and also any of the numbers 671, 256, 35 is an integer. Now we can easily give examples of integers. For example, the number 38 is an integer, the number 70040 is also an integer, zero is an integer (recall that zero is NOT a natural number, zero is an integer), the numbers 999, 1, 8, 93, 832 are also examples of integers. All integers numbers are conveniently represented as a sequence of integers, which has the following form: 0, 1, 2, 3, ... A sequence of integers can be written like this: ..., 0, 1547, 562, 35554,457,666,345,878,789. From the definition of integers, it follows that the set of natural numbers is a subset of the set of integers. Therefore, any natural number is an integer, but not any integer is a natural number. ".

I tried to do this:

file1 = open('test.txt', 'r') text1 = file1.read() import re even_num = [] new_t=[] for int_numbers in range(0, len(re.findall(r"[0-9]+", text1))): if int(re.findall(r"[0-9]+", text1)[int_numbers]) % 2 == 0: even_num.append(re.findall(r"[0-9]+", text1)[int_numbers]) for iii in range(0,len(re.split(r"\s|\,", text1))-1): if re.split(r"\s|\,", text1)[iii] not in even_num: new_t.append(re.split(r"\s|\,", text1)[iii]) print(new_t) 

As a result, all the even numbers were deleted, but I had difficulty with writing the result in the same file in the same format. Tell me, please.

  • Read the file into an array, remove all even numbers from it, save the array to a file. Which of these three subtasks are you having trouble with? Or do everything entirely for you? - AK
  • delete all even numbers - Valeri
  • I read your comments under an attempt to answer "at random." Give an example of the input in question. - AK
  • I’m trying to remove all even numbers from the text: "The definition of integers states that any of the numbers 1, 2, 3, 4, the number 0, and any of the numbers 671, 256, 35 is integer. Now we can easily give examples integers. For example, the number 38 is an integer, the number 70040 is also an integer, zero is an integer (recall that zero is NOT a natural number, zero is an integer), the numbers 999, 1, 8, 93, 832, are also examples of integers " - Valeri
  • Please put your example in the text of the question: all essential parts of the question should be in the question itself, not in the comments. - AK

1 answer 1

I would venture to suggest that the numbers in the file are written line by line (there is one number on each line). Then so:

 f = open('file.txt') line = f.readline() while line: try: if int(line) % 2 == 0: # это чётное число print(line) else: # это нечётное число pass except Exception: continue f.close() 

Then you can do anything with these numbers. As an option, collect all the odd in the list and then throw in the file.

  • There the text is mixed up with numbers, I try through regular expressions, but I hit the bar ... - Valery
  • four
    @ Valery sample code and sample data to the studio - Captain Flint
  • The definition of integers states that any of the numbers 1, 2, 3, 4, the number 0, and also any of the numbers 671, 256, 35 is integer. Now we can easily give examples of integers. For example, the number 38 is an integer, the number 70040 is also an integer, zero is an integer (recall that zero is NOT a natural number, zero is an integer), the numbers 999, 1, 8, 93, 832 are also examples of integers . - Valery
  • @ Valery and ..? Further? - Captain Flint
  • file1 = open ('test.txt', 'r') text1 = file1.readlines () import re patt = r "[0-9] +" rrr = re.findall (patt, text1 [0]) # all integers numbers for the first line - Valeri