Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8414053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:04:27+00:00 2026-06-10T01:04:27+00:00

I have this code: from BeautifulSoup import BeautifulSoup TABLE_CONTENT = [[‘958′,'<a id=958F href=javascript:c_row(\’958\’) title=go

  • 0

I have this code:

from BeautifulSoup import BeautifulSoup

TABLE_CONTENT = [['958','<a id="958F" href="javascript:c_row(\'958\')" title="go to map"><img src="/images/c_map.png" border="0"></a>','USA','Atmospheric','<a href="javascript:c_ol(\'958\')" title="click date time to show origin_list (evid=958)">1945/07/16 11:29:45</a>','33.6753','-106.4747','','-.03','21','','','TRINITY','&nbsp;','&nbsp;','<a href="javascript:c_md(\'958\')" title="click here to show source data">SourceData</a>','&nbsp;'],['959','<a id="959F" href="javascript:c_row(\'959\')" title="go to map"><img src="/images/c_map.png" border="0"></a>','USA','Atmospheric','<a href="javascript:c_ol(\'959\')" title="click date time to show origin_list (evid=959)">1945/08/05 23:16:02</a>','34.395','132.4538','','-.58','15','','','LITTLEBOY','&nbsp;','&nbsp;','<a href="javascript:c_md(\'959\')" title="click here to show source data">SourceData</a>','&nbsp;']]

EVENT_LIST = []
for EVENT in TABLE_CONTENT:
    events = {}
    for index, item in enumerate(EVENT):
        if index == 0:
            events['id'] = item
        if index == 4:
            soup = BeautifulSoup(item)
            for a in soup.findAll('a'):
                events['date'] = ''.join(a.findAll(text=True))
        if index == 2:
            events['country'] = item
        if index == 3:
            events['type'] = item
        if index == 5:
            events['lat'] = item
        if index == 6:
            events['lon'] = item
        if index == 8:
            events['depth'] = item
        if index == 9:
            events['yield'] = item
        if index == 12:
            events['name'] = item
    sorted(events, key=lambda key: events['id'])
    EVENT_LIST.append(events)
    print '=== new record ==='
EVENT_LIST.sort(key=lambda x: x['id'])
print EVENT_LIST

the first issue, i have is that within the EVENT_LIST the dictionary objects are not in the same order as they have been added to the list, for example, the ‘lat’ and ‘lon’ when i print the results are not in order:

[{'name': 'TRINITY', 'country': 'USA', 'lon': '-106.4747', 'yield': '21', 'lat': '33.6753', 'depth': '-.03', 'date': u'1945/07/16 11:29:45', 'type': 'Atmospheric', 'id': '958'}, {'name': 'LITTLEBOY', 'country': 'USA', 'lon': '132.4538', 'yield': '15', 'lat': '34.395', 'depth': '-.58', 'date': u'1945/08/05 23:16:02', 'type': 'Atmospheric', 'id': '959'}]

also is there a better way to write this code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:04:28+00:00Added an answer on June 10, 2026 at 1:04 am

    You can preserve the order of insertions into a dictionary by using an OrderedDict container. From the manual:

    Return an instance of a dict subclass, supporting the usual dict
    methods. An OrderedDict is a dict that remembers the order that keys
    were first inserted. If a new entry overwrites an existing entry, the
    original insertion position is left unchanged. Deleting an entry and
    reinserting it will move it to the end.

    This feature has only been around since version 2.7.

    @Better way: You might change subsequent if index == ... to elif index == ... since, if the index is 2, it never can be 5. Or you could store index/key combinations and use those to store your items. Example (not tried):

    combos={
            0: 'id',
            2: 'country',
            3: 'type',
            5: 'lat',
            6: 'lon',
            8: 'depth',
            9: 'yield',
            12: 'name' }
    
    ...
    
    for index, item ...:
        if index == 4:
            soup = BeautifulSoup(item)
            for a in soup.findAll('a'):
            events['date'] = ''.join(a.findAll(text=True))
        elif index in combos:
            events[combox[index]]=item
    

    I think you get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that fetches some text from a page using BeautifulSoup soup=
I have this Javascript code I inherited from another developer. I am very new
I have this code in my forms.py : from django import forms from formfieldset.forms
I have this code which depends on this javascript from dynamic drive: http://dynamicdrive.com/dynamicindex16/formdependency.htm Its
I have the following simple code: import urllib2 import sys sys.path.append('../BeautifulSoup/BeautifulSoup-3.1.0.1') from BeautifulSoup import
I have this code from a javascript /+\uFF0B0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9u/ after some read about php &
I have this script: import urllib2 from BeautifulSoup import BeautifulSoup import html5lib import lxml
i have this code from t he django sample tutorial from django.db import models
I have following python code: def scrapeSite(urlToCheck): html = urllib2.urlopen(urlToCheck).read() from BeautifulSoup import BeautifulSoup
I have this code from win32com.client import Dispatch connection_string = Provider=SQLNCLI;server=%s;initial catalog=%s;user id=%s;password=%s%(server,db_name,user,pwd) dbConn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.