Imported string in dataframe not the same as same string assigned to variable

Viewed 30

I need to import a excel file - either .csv or .xlsx - and then use that data to update some fields on a website. However, I am running into this problem where the data that I import is not the same as what I might have imported as a string.

I've tried .csv and .xlsx, and both file formats give me the same problem.

I'm using selenium to get the data from a field on a website.

search = driver.find_element(By.ID,'comments')
comment_text = search.get_attribute("value")

# Comment text will display something generic like:
comment_text = 'This is a comment. This is an addition test. This is an addition test. \nThis confirms that I can add comments.\nThis comment should be different from the others in the field. It is a test to make sure everything is alright.'

I import my file:

request_list = pd.read_excel(r'PATH\request_comms.xlsx')

Which looks similar to this

Requests Comment
0 11001 This comment should be different...
1 12012 Request needs to be updated by...
2 13012 NaN

If I set the string to a variable and test if it exists as a substring:

comment = "This comment should be different from the others in the field. It is a test to make sure everything is alright."

if comment in comment_text:
    print(True)
else:
    print(False)

It will print True.

However, if I try with the imported string:

if request_list['Comment'][0] in comment_text:
    print(True)
else:
    print(False)

It will print False

If I compare the two strings

if request_list['Comment'][0] == comment:
    print(True)
else:
    print(False)

It will print False.

The strings are typed exactly the same. I have also created my own dataframe with the comment, and that worked.

df = pd.DataFrame(data={'Comment': comment})

if df['Comment'][0] in comment_text:
    print(True)
else:
    print(False)

Which prints True.

So the only different seems to be importing the file. Does anybody know why this would be the case? In the excel and csv files both were set to text, and the dataframe type is string.

Edit: I also tested the two strings this way:

first_set = set(request_list['Comment'][0])
second_set = set(comment)
difference = first_set.symmetric_difference(second_set)
print(difference)

And got "{'m', 'c', 'b', 'g', 'y', 'r', 'k', 't', 'f', 's', 'o', 'This comment should be different from the others in the field. It is a test to make sure everything is alright.', 'u', 'n', 'd', 'T', 'h', 'I', 'i', ' ', 'e', 'l', '.', 'v', 'a'}" as my output.

0 Answers
Related