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

Closed
M0hamedTohamy opened this issue Nov 25, 2024 · 1 comment
Closed

Script python JsoN Csv #691

M0hamedTohamy opened this issue Nov 25, 2024 · 1 comment
Labels
stale There has not been activity on this issue or PR for quite some time.

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}")

Copy link

There hasn't been any activity on this issue recently, so we clean up some of the older and inactive issues.
Please make sure to update to the latest version and check if that solves the issue. Let us know if that works for you by leaving a comment 👍
This issue has now been marked as stale and will be closed if no further activity occurs. Thanks!

@github-actions github-actions bot added the stale There has not been activity on this issue or PR for quite some time. label Dec 26, 2024
@frenck frenck closed this as not planned Won't fix, can't repro, duplicate, stale Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale There has not been activity on this issue or PR for quite some time.
Projects
None yet
Development

No branches or pull requests

2 participants