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 7619325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:39:34+00:00 2026-05-31T03:39:34+00:00

Possible Duplicate: Parsing blank XML tags with LXML and Python Given the following XML

  • 0

Possible Duplicate:
Parsing blank XML tags with LXML and Python

Given the following XML file:

<Car>
    <Color>Blue</Color>
    <Make>Chevy</Make>
    <Model/>
</Car>

and using the following code:

Car = element.xpath('//Root/Foo/Bar/Car/node()')
parsedCarData = [{field.tag: field.text for field in Car} for action in Car]

How can I replace key values of type None with a blank string so I can use parsed key values into a MySQL statement.

My Attempt – Thanks to Marcin
Using a turnary seemed to work

parsedCars = [{field.tag: field.text if isinstance(field.text,str) else '' for field in Cars} for action in Cars]

For my cases it has always evaluated as true.However when outputting a previous None type is outputs twice. Improvements are welcome!

  • 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-31T03:39:36+00:00Added an answer on May 31, 2026 at 3:39 am

    Given your requirement, you might be better off filtering on is None, but in practice, I doubt this will make a difference:

    parsedCars = [{field.tag: field.text if field.text is not None else '' for field in Cars} for action in Cars]
    

    You should probably use basestr instead of str, though, because your code will replace all instances of unicode with '', should lxml ever switch to unicode.

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

Sidebar

Related Questions

Possible Duplicate: Parsing XML in Cocoa iPhone - Can we parse .plist(xml) file using
Possible Duplicate: Parsing text in C Say I have written to a text file
Possible Duplicate: When is it OK to use an XML file to save information?
Possible Duplicate: Parsing Dates and Times from Strings using python I'm reading a string
Possible Duplicate: Comparison of XML parsing APIs on the Java SE platform I am
Possible Duplicate: Parsing CSV in java How to do I parse a csv file
Possible Duplicate: Parsing HTML in Python I have a long string of HTML similar
Possible Duplicate: generically parsing String to date Following situation: I need to detect if
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: CSV parsing in Java - working example..? I have a list of

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.