Python Forum
TypeError: 're.Match' object is not iterable
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: 're.Match' object is not iterable
#1
I was writing the following Python Code for copying search results (phone numbers) to a new .txt file and I got a TypeError. Please what other way can use to copy the "match" in the for loop to a new file?

Please help you if you can... Thanks!

import re pattern = re.compile(r"\+\d\d\d\d\d\d\d\d\d\d\d\d\d") with open("Tunj1.txt", "r") as wadb: init_content = wadb.read() matches = pattern.finditer(init_content) for match in matches: print(match) with open("Extracted Numbers.txt", "w") as wadb_Extracted: for line in match: wadb_Extracted.write(line)

Error:
Traceback (most recent call last): File "C:/Users/Charles Auspicks/PycharmProjects/HelloWorld/app.py", line 14, in <module> for line in match: TypeError: 're.Match' object is not iterable
Reply
#2
matches is an iterator that returns Match objects.

Match objects themselves have information inside, but are not themselves iterable. So when you do for line in match, the iteration attempt fails.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  cannot unpack non-iterable int object in urllib3/util/wait.py", line 85, ping_chen_ibm_us 2 882 Aug-01-2025, 02:05 PM
Last Post: ping_chen_ibm_us
  Did you know that map() accepts more than one iterable? DeaD_EyE 2 1,409 Apr-13-2025, 10:16 AM
Last Post: noisefloor
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 4,815 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
Question TypeError: argument of type 'NoneType' is not iterable Tajaldeen 7 6,116 Nov-29-2024, 09:45 AM
Last Post: Tajaldeen
  I am getting this TypeError: 'TreasureMap' object is not subscriptable. makilakos 2 2,098 May-25-2024, 07:58 PM
Last Post: deanhystad
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 4,247 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 2,848 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 5,069 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 19,655 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 8,390 Aug-23-2023, 06:21 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.