Search code examples
pythonpython-2.7xlrd

TypeError: coercing to Unicode: need string or buffer, Cell found


I try to use xlrd to load some records from excel and check the relationship. Please refer to my amateur codes for more details:

import xlrd

feature_list_file = xlrd.open_workbook('FeatureList.xls')
feature_table = feature_list_file.sheet_by_index(0)

num_feature_rows = feature_table.nrows
num_feature_cols = feature_table.ncols

feature_list = []

for i in range(num_feature_rows):
    feature_list.append(feature_table.cell(i, 1))
# print feature_list

issue_list_file = xlrd.open_workbook('IssueList.xls')
issue_table = issue_list_file.sheet_by_index(0)

num_issue_rows = issue_table.nrows
num_issue_cols = issue_table.ncols

epic_list = []

for i in range(num_issue_rows):
    if issue_table.cell(i, 0).value == 'Epic':
        epic_list.append(issue_table.cell(i, 1).value)

# print epic_list


def check_link(actual_link, parent_list):
    result = True
    for i in range(parent_list.__len__()):
        count = 0
        if parent_list[i] in actual_link:
            count += 1
            if count > 1:
                result = False
                break
    return result


invalid_list = []

for i in range(num_issue_rows):
    if issue_table.cell(i, 10).value == '':
        invalid_list.append(issue_table.cell(i, 1).value)
    else:
        if issue_table.cell(i, 0).value == 'Story':
            if check_link(issue_table.cell(i, 10).value, epic_list):
                invalid_list.append(issue_table.cell(i, 1).value)
        if issue_table.cell(i, 0).value == 'Epic':
            if check_link(issue_table.cell(i, 10).value, feature_list):
                invalid_list.append(issue_table.cell(i, 1).value)

print invalid_list

However, it always returns below messages

Traceback (most recent call last):
  File "/Users/sut/PycharmProjects/ItemChecker/JiraItemChecker.py", line 54, in <module>
    if check_link(issue_table.cell(i, 10).value, feature_list):
  File "/Users/sut/PycharmProjects/ItemChecker/JiraItemChecker.py", line 36, in check_link
    if parent_list[i] in actual_link:
TypeError: coercing to Unicode: need string or buffer, Cell found

How could I resolve this issue?

Thanks


Solution

  • you're trying to do an in on an xlrd.cell object, I think you need to change

    if parent_list[i] in actual_link:
    

    to

    if parent_list[i].value in actual_link:
    

    adding some more details to my answer based on your comment

    Your call is passing what appears to be a string and a list of cells the string is fine, but in your function you iterate over the list and try to compare each instance of the cell class to the correctly passed string, and there's your problem.

    hope this helps (and I'm not misreading the code!)

    and some more details -

    here's the crux of the problem, when you create the feature_list, and the epic_list you are doing it in 2 different ways, on the first you are appending a cell to the list, on the second you are appending a value. BUT then you are using the same check_link function on both types. So either you need to extend your check_link function to handle both types, OR you need to be consistent and pick one.