How To Detect If A Cell Is Empty When Reading Excel Files Using The ...

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs How to detect if a cell is empty when reading Excel files using the xlrd library? Ask Question Asked 12 years, 4 months ago Modified 12 years, 4 months ago Viewed 31k times 11

I handle Excel files using the functions row_values and col_values:

import xlrd workbook = xlrd.open_workbook( filename ) sheet_names = workbook.sheet_names() for sheet_name in sheet_names: sheet = workbook.sheet_by_name( sheet_name ) # ... row_values = sheet.row_values( rownum ) # ... col_values = sheet.col_values( colnum )

For example, I get col_values as list. What if I meet an empty cell in some column? For example a cell (1,1) is not empty, a cell (1,2) is empty and a cell (1,3) is not empty? How can I detect that the cell (1,2) is empty?

Is this true that I get a list with an empty string as a value of an empty cell (for most well-known programs which generate Excel files)?

Share Improve this question Follow edited Jul 22, 2012 at 19:18 sergzach asked Jul 22, 2012 at 18:43 sergzach's user avatar sergzachsergzach 6,7547 gold badges51 silver badges86 bronze badges Add a comment |

1 Answer 1

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 19

You could be explicit and check that sheet.cell_type(rowno, colno) in (xlrd.XL_CELL_EMPTY, xlrd.XL_CELL_BLANK) but the docs state the value will be u'' where those are the case anyway.

Instead of using row_values, you could also use row(n) which returns a list of Cell objects which have .value and .cell_type attributes.

Share Improve this answer Follow answered Jul 22, 2012 at 20:30 Jon Clements's user avatar Jon ClementsJon Clements 142k34 gold badges252 silver badges286 bronze badges 2
  • This is almost 8 years later and I'm getting 'Cell' object has no attribute 'cell_type' – Ali Tou Commented Nov 18, 2020 at 10:49
  • @AliTou this may be a roundabout way, but I just convert the cell value to a string and compare it to an empty string (e.g cell_value = str(cell.value); cell_empty = (cell_value == "")) – jack.py Commented Dec 20, 2020 at 6:56
Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • AI agents that help doctors get paid
  • Featured on Meta
  • The Winter 2024 Community Asks Sprint has been moved to to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries
Visit chat

Linked

5 How to stop reading a spreadsheet with xlrd at the first empty row? 2 python: xlrd/csv - empty cell treatment when xlrd/csv packages read objects into memory 0 XLRD Out of Range Error 1 How to skip empty .xls cells in loop : XLRD (Python) 3 Detecting Blank Excel Cell using python 7 How to check if valid excel file in python xlrd library 1 Python - Loop through non-blank Excel cells 2 Checking for empty cells with OpenPyXl 8 pandas data frame read_excel: How to look for empty cells? 14 How to check if .xls and .csv files are empty 5 How to stop reading a spreadsheet with xlrd at the first empty row? 1 Check if a Excel Sheet is empty 1 Check for empty excel cell (Python) 0 Reading contents of an Excel spreadsheet that includes empty cells using Python. I need to programmatically ignore empty cells

Hot Network Questions

  • Why is the speed graph of a survey flight a square wave?
  • Useful aerial recon vehicles for newly colonized worlds
  • Does the causal closure principle necessitate the non causal role of consciousness?
  • Chain falls behind rear sprockets - safeguards?
  • Why are an F-35’s missile rails angled outboard?
  • Role of thrust during take off
  • Creating "horseshoe" polylines from lines in QGIS
  • Which lubricant for plastic rail guides on sliding doors?
  • I made a Betty Crocker cake mix with oil instead of butter - how to fix it?
  • Other than impedance, what should determine the selection of R and C in a low-pass or high-pass filter?
  • Why are the walls of a spacecraft usually so thin?
  • Laptop sometimes hard reboots when scrolling in Twitter
  • VBE multiplier with BJTs?
  • PHP7.4 ldap broken on focal after 13 dec 2024
  • breaking lines of a lengthy equation in a multiline bracket using equation*
  • How do I see that the different definitions for SYK are equivalent?
  • Why can`t DSolve solve this second order ode with initial conditions?
  • Was angling tank armor a recognized doctrine during World War II?
  • Why is there no strong contrast between inside and outside the tunnel in my Blender animation?
  • Why are CHACHA20 TLS ciphers not compliant with the NIST guidelines and FIPS/HIPAA standards?
  • Print the largest hidden double
  • How can dragons heat their breath?
  • In PhD applications, how should I deal with a MSc supervisor who gives unfairly negative recommendation letters?
  • Is the uncertainty principle a consequence of classical reference frame?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-py

Từ khóa » Thư Viện Xlrd