דילוג לתוכן
  • חוקי הפורום
  • לא נפתר
  • משתמשים
  • חיפוש גוגל בפורום
  • צור קשר
עיצובים
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • ברירת מחדל (ללא עיצוב (ברירת מחדל))
  • ללא עיצוב (ברירת מחדל)
כיווץ
לוגו מותג
  1. דף הבית
  2. קטגוריות בהרצה
  3. תכנות
  4. Python
  5. עזרה הדדית - Python
  6. עזרה | חילוץ מידע מקובץ json בpython

עזרה | חילוץ מידע מקובץ json בpython

מתוזמן נעוץ נעול הועבר נפתר עזרה הדדית - Python
7 פוסטים 4 כותבים 202 צפיות 3 עוקבים
  • מהישן לחדש
  • מהחדש לישן
  • הכי הרבה הצבעות
תגובה
  • תגובה כנושא
התחברו כדי לפרסם תגובה
נושא זה נמחק. רק משתמשים עם הרשאות מתאימות יוכלו לצפות בו.
  • האדם החושבה מנותק
    האדם החושבה מנותק
    האדם החושב
    מדריכים
    כתב ב נערך לאחרונה על ידי האדם החושב
    #1

    יש לי את הקובץ הזה:
    https://github.com/zevisvei/Sefaria-Export/blob/master/table_of_contents.json
    אני צריך לחלץ את הheShortDesc לפי הheTitle,
    ניסיתי את הסקריפט הזה:

    import json
    
    def get_content_from_json(file_path):
        with open(file_path, 'r', encoding='utf-8') as file:
            return json.load(file)
    
    def find_he_short_desc(entry, target_title):
        if 'heTitle' in entry and target_title in entry['heTitle']:
            return entry.get('heShortDesc')
        elif 'contents' in entry:
            return next((find_he_short_desc(sub_entry, target_title) for sub_entry in entry['contents']), None)
    
    table_of_contents_file = "table_of_contents.json"
    table_of_contents = get_content_from_json(file_path=table_of_contents_file)
    
    target_title = input("Enter heTitle to find heShortDesc: ").strip()
    
    found_he_short_desc = next((find_he_short_desc(entry, target_title) for entry in table_of_contents), None)
    
    if found_he_short_desc:
        print(f"The heShortDesc for {target_title} is: {found_he_short_desc}")
    else:
        print(f"{target_title} not found in the table of contents.")
    

    אבל הוא לא שואב מכל הרמות, והוא גם עושה בעיות בשמות עם / ו" באמצע הטקסט
    בתודה מראש.
    @תכנות

    צדיק תמיםצ MGM IVRM 2 תגובות תגובה אחרונה
    0
    • האדם החושבה האדם החושב

      יש לי את הקובץ הזה:
      https://github.com/zevisvei/Sefaria-Export/blob/master/table_of_contents.json
      אני צריך לחלץ את הheShortDesc לפי הheTitle,
      ניסיתי את הסקריפט הזה:

      import json
      
      def get_content_from_json(file_path):
          with open(file_path, 'r', encoding='utf-8') as file:
              return json.load(file)
      
      def find_he_short_desc(entry, target_title):
          if 'heTitle' in entry and target_title in entry['heTitle']:
              return entry.get('heShortDesc')
          elif 'contents' in entry:
              return next((find_he_short_desc(sub_entry, target_title) for sub_entry in entry['contents']), None)
      
      table_of_contents_file = "table_of_contents.json"
      table_of_contents = get_content_from_json(file_path=table_of_contents_file)
      
      target_title = input("Enter heTitle to find heShortDesc: ").strip()
      
      found_he_short_desc = next((find_he_short_desc(entry, target_title) for entry in table_of_contents), None)
      
      if found_he_short_desc:
          print(f"The heShortDesc for {target_title} is: {found_he_short_desc}")
      else:
          print(f"{target_title} not found in the table of contents.")
      

      אבל הוא לא שואב מכל הרמות, והוא גם עושה בעיות בשמות עם / ו" באמצע הטקסט
      בתודה מראש.
      @תכנות

      צדיק תמיםצ מנותק
      צדיק תמיםצ מנותק
      צדיק תמים
      מדריכים
      כתב ב נערך לאחרונה על ידי
      #2

      @האדם-החושב אתה רוצה ללמוד איך לעשות את זה בפייתון? או שחשובה לך רק התוצאה?

      רוצה לזכור קריאת שמע בזמן? לחץ כאן! || אתר שכולו מדריכים

      האדם החושבה תגובה 1 תגובה אחרונה
      1
      • צדיק תמיםצ צדיק תמים

        @האדם-החושב אתה רוצה ללמוד איך לעשות את זה בפייתון? או שחשובה לך רק התוצאה?

        האדם החושבה מנותק
        האדם החושבה מנותק
        האדם החושב
        מדריכים
        כתב ב נערך לאחרונה על ידי
        #3

        @צדיק-תמים חשובה לי בעיקר התוצאה, אבל אם אני אלמד משהו על הדרך זה לא יזיק...

        תגובה 1 תגובה אחרונה
        0
        • האדם החושבה האדם החושב

          יש לי את הקובץ הזה:
          https://github.com/zevisvei/Sefaria-Export/blob/master/table_of_contents.json
          אני צריך לחלץ את הheShortDesc לפי הheTitle,
          ניסיתי את הסקריפט הזה:

          import json
          
          def get_content_from_json(file_path):
              with open(file_path, 'r', encoding='utf-8') as file:
                  return json.load(file)
          
          def find_he_short_desc(entry, target_title):
              if 'heTitle' in entry and target_title in entry['heTitle']:
                  return entry.get('heShortDesc')
              elif 'contents' in entry:
                  return next((find_he_short_desc(sub_entry, target_title) for sub_entry in entry['contents']), None)
          
          table_of_contents_file = "table_of_contents.json"
          table_of_contents = get_content_from_json(file_path=table_of_contents_file)
          
          target_title = input("Enter heTitle to find heShortDesc: ").strip()
          
          found_he_short_desc = next((find_he_short_desc(entry, target_title) for entry in table_of_contents), None)
          
          if found_he_short_desc:
              print(f"The heShortDesc for {target_title} is: {found_he_short_desc}")
          else:
              print(f"{target_title} not found in the table of contents.")
          

          אבל הוא לא שואב מכל הרמות, והוא גם עושה בעיות בשמות עם / ו" באמצע הטקסט
          בתודה מראש.
          @תכנות

          MGM IVRM מנותק
          MGM IVRM מנותק
          MGM IVR
          מדריכים
          כתב ב נערך לאחרונה על ידי MGM IVR
          #4

          @האדם-החושב כתב בעזרה | חילוץ מידע מקובץ json בpython:

          אני צריך לחלץ את הheShortDesc לפי הheTitle,

          לא הבנתי מה התוצאה הרצויה, כייון שלא צירפת דוגמא לאיך זה אמור להיראות,
          אבל אם אתה צריך משהו כזה

          [
            {
              "בראשית": "בריאת העולם, תחילתה של האנושות וסיפורי האבות והאמהות."
            },
            {
              "שמות": "שעבוד בני ישראל במצרים, יציאת מצרים, מתן תורה ובניית המשכן."
            },
            {
              "ויקרא": "עבודת הקורבנות במשכן, דיני טהרה ומצוות אחרות העוסקות בעבודה חקלאית, בהתנהגות חברתית, ובמועדים."
            },
            {
              "במדבר": "מסעות בני ישראל במדבר, מפקדי אוכלוסין, מרידות, חטא המרגלים, מלחמות ושלל חוקים."
            },
            {
              "דברים": "נאומי משה האחרונים, חזרה על אירועי המדבר, סקירה של חוקים שנאמרו בספרים הקודמים, הצגת חוקים חדשים וקריאה לנאמנות לאלוהים."
            }]
          

          זה קוד שיעבוד לך

          import requests
          import json
          url = 'https://raw.githubusercontent.com/zevisvei/Sefaria-Export/master/table_of_contents.json'
          
          r = requests.get(url)
          alldata = r.json()
          response = []
          for i1 in alldata:
              for i2 in i1['contents']:
                  if i2.get('contents') is not None:
                      for i3 in i2['contents']:
                          if i3.get('contents') is not None:
                              for i4 in i3['contents']:
                                  if i4.get('contents') is not None:
                                      for i5 in i4['contents']:
                                          if i5.get('contents') is not None:
                                              for i6 in i5['contents']:
                                                  if i6.get('contents') is not None:
                                                      for i7 in i6['contents']:
                                                          if i7.get('contents') is not None:
                                                              for i8 in i7['contents']:
                                                                  heTitle = i8['heTitle']
                                                                  heShortDesc = i8['heShortDesc']
                                                                  response.append({heTitle:heShortDesc})
                                                          else:
                                                              if i7.get('heTitle') is None:
                                                                  continue
                                                              heTitle = i7['heTitle']
                                                              heShortDesc = i7['heShortDesc']
                                                              response.append({heTitle:heShortDesc})
                                                  else:
                                                      if i6.get('heTitle') is None:
                                                          continue
                                                      heTitle = i6['heTitle']
                                                      heShortDesc = i6['heShortDesc']
                                                      response.append({heTitle:heShortDesc})
                                          else:
                                              if i5.get('heTitle') is None:
                                                  continue
                                              heTitle = i5['heTitle']
                                              heShortDesc = i5['heShortDesc']
                                              response.append({heTitle:heShortDesc})
                                  else:
                                      if i4.get('heTitle') is None:
                                          continue
                                      heTitle = i4['heTitle']
                                      heShortDesc = i4['heShortDesc']
                                      response.append({heTitle:heShortDesc})
                          else:
                              if i3.get('heTitle') is None:
                                  continue
                              heTitle = i3['heTitle']
                              heShortDesc = i3['heShortDesc']
                              response.append({heTitle:heShortDesc})
                  else:
                      if i2.get('heTitle') is None:
                          continue
                      heTitle = i2['heTitle']
                      heShortDesc = i2['heShortDesc']
                      response.append({heTitle:heShortDesc})
          
              
              
          open('table_of_contents.json', 'w').write(json.dumps(response, ensure_ascii=False, indent=2))
          

          יכול להיות שאפשר לעשות את זה יותר קצר

          אם אתה צריך משהו אחר, תעדכן

          האדם החושבה תגובה 1 תגובה אחרונה
          3
          • MGM IVRM MGM IVR

            @האדם-החושב כתב בעזרה | חילוץ מידע מקובץ json בpython:

            אני צריך לחלץ את הheShortDesc לפי הheTitle,

            לא הבנתי מה התוצאה הרצויה, כייון שלא צירפת דוגמא לאיך זה אמור להיראות,
            אבל אם אתה צריך משהו כזה

            [
              {
                "בראשית": "בריאת העולם, תחילתה של האנושות וסיפורי האבות והאמהות."
              },
              {
                "שמות": "שעבוד בני ישראל במצרים, יציאת מצרים, מתן תורה ובניית המשכן."
              },
              {
                "ויקרא": "עבודת הקורבנות במשכן, דיני טהרה ומצוות אחרות העוסקות בעבודה חקלאית, בהתנהגות חברתית, ובמועדים."
              },
              {
                "במדבר": "מסעות בני ישראל במדבר, מפקדי אוכלוסין, מרידות, חטא המרגלים, מלחמות ושלל חוקים."
              },
              {
                "דברים": "נאומי משה האחרונים, חזרה על אירועי המדבר, סקירה של חוקים שנאמרו בספרים הקודמים, הצגת חוקים חדשים וקריאה לנאמנות לאלוהים."
              }]
            

            זה קוד שיעבוד לך

            import requests
            import json
            url = 'https://raw.githubusercontent.com/zevisvei/Sefaria-Export/master/table_of_contents.json'
            
            r = requests.get(url)
            alldata = r.json()
            response = []
            for i1 in alldata:
                for i2 in i1['contents']:
                    if i2.get('contents') is not None:
                        for i3 in i2['contents']:
                            if i3.get('contents') is not None:
                                for i4 in i3['contents']:
                                    if i4.get('contents') is not None:
                                        for i5 in i4['contents']:
                                            if i5.get('contents') is not None:
                                                for i6 in i5['contents']:
                                                    if i6.get('contents') is not None:
                                                        for i7 in i6['contents']:
                                                            if i7.get('contents') is not None:
                                                                for i8 in i7['contents']:
                                                                    heTitle = i8['heTitle']
                                                                    heShortDesc = i8['heShortDesc']
                                                                    response.append({heTitle:heShortDesc})
                                                            else:
                                                                if i7.get('heTitle') is None:
                                                                    continue
                                                                heTitle = i7['heTitle']
                                                                heShortDesc = i7['heShortDesc']
                                                                response.append({heTitle:heShortDesc})
                                                    else:
                                                        if i6.get('heTitle') is None:
                                                            continue
                                                        heTitle = i6['heTitle']
                                                        heShortDesc = i6['heShortDesc']
                                                        response.append({heTitle:heShortDesc})
                                            else:
                                                if i5.get('heTitle') is None:
                                                    continue
                                                heTitle = i5['heTitle']
                                                heShortDesc = i5['heShortDesc']
                                                response.append({heTitle:heShortDesc})
                                    else:
                                        if i4.get('heTitle') is None:
                                            continue
                                        heTitle = i4['heTitle']
                                        heShortDesc = i4['heShortDesc']
                                        response.append({heTitle:heShortDesc})
                            else:
                                if i3.get('heTitle') is None:
                                    continue
                                heTitle = i3['heTitle']
                                heShortDesc = i3['heShortDesc']
                                response.append({heTitle:heShortDesc})
                    else:
                        if i2.get('heTitle') is None:
                            continue
                        heTitle = i2['heTitle']
                        heShortDesc = i2['heShortDesc']
                        response.append({heTitle:heShortDesc})
            
                
                
            open('table_of_contents.json', 'w').write(json.dumps(response, ensure_ascii=False, indent=2))
            

            יכול להיות שאפשר לעשות את זה יותר קצר

            אם אתה צריך משהו אחר, תעדכן

            האדם החושבה מנותק
            האדם החושבה מנותק
            האדם החושב
            מדריכים
            כתב ב נערך לאחרונה על ידי האדם החושב
            #5

            @MGM-IVR אני רוצה שכשאני מכניס input של טקסט שנמצא בheTitle הוא יחזיר לי את הערך שנמצא בheShortDesc
            אני צריך את זה כדי לשלב את זה בתוך סקריפט אחר שאמור לקרוא לפונקצייה עם ארגומנט של הheTitle והreturn של הפונקציה יהיה הטקסט של הheShortDesc, אני לא רוצה לשנות את הקובץ עצמו.
            אני צריך משהו בסגנון הזה:

            import json
            
            
            def process_data(data, heTitle):
                for item in data:
                    if type(item) != "str":
                        if type(item) == "dict":
                            if item.get("heTitle") == heTitle:
                                heShortDesc = item.get("heShortDesc")
                                return heShortDesc
                            else:
                                process_data(item, heTitle)
                        else:
                            process_data(item, heTitle)
                
            
            json_file = "table_of_contents.json"
            heTitle = input()
            
            with open('table_of_contents.json', 'r', encoding='utf-8') as file:
                json_data = json.load(file)
                process_data(json_data, heTitle)
            

            אבל בלי הבעיה של הרקורסיביות
            ניסיתי גם כך:

            import json
            
            def process_data(data, heTitle):
                if isinstance(data, dict):
                    if data.get("heTitle") == heTitle:
                        heShortDesc = data.get("heShortDesc")
                        print(heShortDesc)
                        return heShortDesc
                    else:
                        for i in data:
                            process_data(i, heTitle)
                            process_data(data.get(i), heTitle)
                elif isinstance(data, list):
                    for list_item in data:
                        process_data(list_item, heTitle)
            
            json_file = "table_of_contents.json"
            heTitle = input()
            
            with open('table_of_contents.json', 'r', encoding='utf-8') as file:
                json_data = json.load(file)
            a = process_data(json_data, heTitle)
            print(a)
            

            הבעיה היא שהפונקצייה מחזירה "none" בכל ריצה שלא מכילה את הheTitle ובכך דורסת את הערך שנמצא, יש למישהו רעיון איך לפתור את זה בלי לחלק את זה ליותר פונקציות?

            sivan22S תגובה 1 תגובה אחרונה
            0
            • האדם החושבה האדם החושב

              @MGM-IVR אני רוצה שכשאני מכניס input של טקסט שנמצא בheTitle הוא יחזיר לי את הערך שנמצא בheShortDesc
              אני צריך את זה כדי לשלב את זה בתוך סקריפט אחר שאמור לקרוא לפונקצייה עם ארגומנט של הheTitle והreturn של הפונקציה יהיה הטקסט של הheShortDesc, אני לא רוצה לשנות את הקובץ עצמו.
              אני צריך משהו בסגנון הזה:

              import json
              
              
              def process_data(data, heTitle):
                  for item in data:
                      if type(item) != "str":
                          if type(item) == "dict":
                              if item.get("heTitle") == heTitle:
                                  heShortDesc = item.get("heShortDesc")
                                  return heShortDesc
                              else:
                                  process_data(item, heTitle)
                          else:
                              process_data(item, heTitle)
                  
              
              json_file = "table_of_contents.json"
              heTitle = input()
              
              with open('table_of_contents.json', 'r', encoding='utf-8') as file:
                  json_data = json.load(file)
                  process_data(json_data, heTitle)
              

              אבל בלי הבעיה של הרקורסיביות
              ניסיתי גם כך:

              import json
              
              def process_data(data, heTitle):
                  if isinstance(data, dict):
                      if data.get("heTitle") == heTitle:
                          heShortDesc = data.get("heShortDesc")
                          print(heShortDesc)
                          return heShortDesc
                      else:
                          for i in data:
                              process_data(i, heTitle)
                              process_data(data.get(i), heTitle)
                  elif isinstance(data, list):
                      for list_item in data:
                          process_data(list_item, heTitle)
              
              json_file = "table_of_contents.json"
              heTitle = input()
              
              with open('table_of_contents.json', 'r', encoding='utf-8') as file:
                  json_data = json.load(file)
              a = process_data(json_data, heTitle)
              print(a)
              

              הבעיה היא שהפונקצייה מחזירה "none" בכל ריצה שלא מכילה את הheTitle ובכך דורסת את הערך שנמצא, יש למישהו רעיון איך לפתור את זה בלי לחלק את זה ליותר פונקציות?

              sivan22S מנותק
              sivan22S מנותק
              sivan22
              כתב ב נערך לאחרונה על ידי
              #6

              @האדם-החושב להלן הפתרון שלי:

              import json
              
              def get_content_from_json(file_path):
                  with open(file_path, 'r', encoding='utf-8') as file:
                      return json.load(file)
                  
              the_dict = {}
              
              def find_he_short_desc(entry):
                  if 'heTitle' in entry and 'heShortDesc' in entry:
                      globals()['the_dict'][entry['heTitle']]= entry['heShortDesc']
                  if 'contents' in entry:
                      for content in entry['contents']:
                          find_he_short_desc(content)
              
              
              table_of_contents_file = "table_of_contents.json"
              table_of_contents = get_content_from_json(file_path=table_of_contents_file)
              for entry in table_of_contents:
                  find_he_short_desc(entry)
              
              
              with open('heTitle_to_heShortDesc.json', 'w', encoding='utf-8') as file:
                  json.dump(the_dict, file, ensure_ascii=False, indent=4)
              
              

              לאחר מכן אתה פשוט משתמש בקובץ שנוצר כמו מילון:

              with open('heTitle_to_heShortDesc.json', 'r', encoding='utf-8') as file:
                  loaded_dict = json.load(file)
              
              def heTitle_to_heShortDesc(heTitle):
                  return loaded_dict[heTitle]
              
              תגובה 1 תגובה אחרונה
              3
              • האדם החושבה מנותק
                האדם החושבה מנותק
                האדם החושב
                מדריכים
                כתב ב נערך לאחרונה על ידי
                #7

                תודה לכל המסייעים, למעשה תיקנתי את הסקריפט, והוא עובד:

                import json
                
                def process_data(data, heTitle, heShortDesc):
                    if isinstance(data, dict):
                        if data.get("heTitle") == heTitle:
                            heShortDesc.append(data.get("heShortDesc"))
                        else:
                            for i in data:
                                process_data(i, heTitle, heShortDesc)
                                process_data(data.get(i), heTitle, heShortDesc)
                    elif isinstance(data, list):
                        for list_item in data:
                            process_data(list_item, heTitle, heShortDesc)
                    return heShortDesc
                
                json_file = "table_of_contents.json"
                heTitle = input()
                
                with open(json_file, 'r', encoding='utf-8') as file:
                    json_data = json.load(file)
                a = process_data(json_data, heTitle, heShortDesc=[])
                print(a)
                
                תגובה 1 תגובה אחרונה
                1
                • האדם החושבה האדם החושב סימן נושא זה כשאלה ב
                • האדם החושבה האדם החושב סימן נושא זה כנפתר ב

                • התחברות

                • אין לך חשבון עדיין? הרשמה

                • התחברו או הירשמו כדי לחפש.
                • פוסט ראשון
                  פוסט אחרון
                0
                • חוקי הפורום
                • לא נפתר
                • משתמשים
                • חיפוש גוגל בפורום
                • צור קשר