Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script python JsoN Csv #691

Open
M0hamedTohamy opened this issue Nov 25, 2024 · 0 comments
Open

Script python JsoN Csv #691

M0hamedTohamy opened this issue Nov 25, 2024 · 0 comments

Comments

@M0hamedTohamy
Copy link

import json
import csv

try:
print("📥 جاري قراءة ملف JSON...")
with open("contacts.json", encoding="utf-8") as json_file:
data = json.load(json_file)
print("✅ تم تحميل بيانات JSON بنجاح.")

print("📤 جاري تحويل البيانات إلى ملف CSV...")
with open("contacts.csv", "w", newline="", encoding="utf-8") as csv_file:
    fieldnames = ["Name", "Phone"]
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

    writer.writeheader()
    for contact in data["contacts"]["list"]:
        name = (f"{contact['first_name']} {contact['last_name']}").strip()
        phone = contact["phone_number"]
        writer.writerow({"Name": name, "Phone": phone})

print("✅ تم تصدير البيانات إلى ملف contacts.csv بنجاح!")

except FileNotFoundError:
print("❌ لم يتم العثور على ملف JSON. تأكد من وجود الملف.")
except KeyError as e:
print(f"❌ خطأ في البيانات: المفتاح {e} غير موجود.")
except json.JSONDecodeError:
print("❌ ملف JSON غير صالح أو يحتوي على خطأ.")
except Exception as e:
print(f"❌ حدث خطأ غير متوقع: {e}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant