0% found this document useful (0 votes)
7 views1 page

2 Json

Uploaded by

caedmon.aldous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

2 Json

Uploaded by

caedmon.aldous
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import os

import json

# Path to the folder containing your txt files


txt_folder_path = 'C:/Users/Administrator/Desktop/umut/zonefiles_cleaned'

# Path to the folder where json files will be saved


json_folder_path = 'C:/Users/Administrator/Desktop/umut/zonefiles_json2'

# Ensure the JSON folder exists


if not os.path.exists(json_folder_path):
os.makedirs(json_folder_path)

# Loop through all files in the txt folder


for filename in os.listdir(txt_folder_path):
if filename.endswith('.txt'):
txt_file_path = os.path.join(txt_folder_path, filename)

# Read lines from txt file


with open(txt_file_path, 'r', encoding='utf-8') as txt_file:
lines = txt_file.readlines()

# Extract the filename without the '.txt' extension


domain_uzantisi = filename.replace('.txt', '')

# Create a list of dictionaries for each line


json_data = [{"domain": line.strip(), "domain_uzantisi": domain_uzantisi}
for line in lines]

# Create the new json file path in the specified json folder
json_filename = filename.replace('.txt', '.json')
json_file_path = os.path.join(json_folder_path, json_filename)

# Write the json data to the new file


with open(json_file_path, 'w', encoding='utf-8') as json_file:
json.dump(json_data, json_file, indent=4)

print(f"Created JSON file: {json_file_path}")

You might also like