gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
I have 300 text files of JSON data. I need to extract all the phone numbers from the…
- User
I have 300 text files of JSON data. I need to extract all the phone numbers from the .txt files. How do I accomplish this quickly?
- Assistant
To extract phone numbers from multiple JSON data text files quickly, you can use a combination of Python and regular expressions. Here's a step-by-step approach:
- Iterate over each text file:
import os directory = 'path/to/text/files' # Replace with the actual directory containing the text files for filename in os.listdir(directory): if filename.endswith('.txt'): with open(os.path.join(directory, filename), 'r') as file: # Continue with the next steps- Read the JSON data from each text file:
import json # Add the following code after 'with open(...)' line in the previous step json_data = json.load(file)- Extract phone numbers using regular expressions:
import re # Add the following code after 'json.load(file)' line in the previous step phone_numbers = re.findall(r"\b\d{10}\b", str(json_data))The above regular expression pattern matches 10 consecutive digits as potential phone numbers. You can modify the pattern to match other phone number formats as needed.