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

  • SEARCH
  • Home
  • 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 1040951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:17:17+00:00 2026-05-16T15:17:17+00:00

I hope I asked that correctly. I am trying to figure out what element.sourceline

  • 0

I hope I asked that correctly. I am trying to figure out what element.sourceline does and if there is some way I can use its features. I have tried building my elements from the html a number of ways but every time I iterate through my elements and ask for sourceline I always get None. When I tried to use the built-in help I done’t get anything either.

I have Googled for an example but none were found yet.

I know it is a method of elements not trees but that is the best I have been able to come up with.

In response to Jim Garrison’s request for an example

theTree=html.parse(open(r'c:\temp\testlxml.htm'))
check_source
the_elements=[(e,e.sourceline) for e in theTree.iter()]  #trying to get the sourceline
for each in the_elements:
    if each[1]!=None:
    check_source.append(each)

When I run this len(check_source)==0

My htm file has 19,379 lines so I am not sure you want to see it

I tried one solution

>>> myroot=html.fromstring(xml)
>>> elementlines=[(e,e.sourceline) for e in myroot.iter()]
>>> elementlines
[(<Element doc at 12bb730>, None), (<Element foo at 12bb650>, None)]

When I do the same thing with etree I get what was demonstrated

>>> myroot=etree.fromstring(xml)
>>> elementlines=[(e,e.sourceline) for e in myroot.iter()]
>>> elementlines
[(<Element doc at 36a6b70>, 1), (<Element foo at 277b4e0>, 2)]

But my source htm is so messy I can’t use etree to explore the tree
I get an error

  • 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-05-16T15:17:18+00:00Added an answer on May 16, 2026 at 3:17 pm

    sourceline will return the line number determined at the time of parsing a document. So it won’t apply to an Element that was added through the API. For example:

    from lxml import etree
    
    xml = '<doc>\n<foo>rain in spain</foo>\n</doc>'
    root = etree.fromstring(xml)
    
    print root.find('foo').sourceline # 2
    
    root.append(etree.Element('bar'))
    print etree.tostring(root)
    print root.find('bar').sourceline # None
    

    I’m pretty sure the same applies to lxml.html.

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

Sidebar

Related Questions

hope someone can help. I appreciate there are a few questions that are similar
Hope you can help. I am importing the current GMT time for an iPhone
hope someone can help me. I have a function like <script language=JavaScript type=text/javascript> function
I've been asked to look at windows service which retrieves data from an Active
I'm writing, or trying to write, Baby's First MDB on WebSphere 7. I have
Hope you guys don't mind me asking this question, but I find myself at
I am all new to rails, however quite familiar with regular MVC programming like
I've used JRab's example here http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/App-specific-system-menu/td-p/693 ... to add a header menu to my
Simple problem, though apparently not a simple solution. Example here: http://myhideout.eu/new/ Basically the site
I want to create a flat result set from the results of two methods

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.