Python Forum

Full Version: .split seemingly gets ignored
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, can you guys see why the .split method is being ignored?

from openpyxl import load_workbook

fb_user_data = load_workbook(filename = './spreadsheets/user_data.xlsx')

# Cell K2: first second third fourth
test_list = fb_user_data['users']['K2'].value
test_list.split()
print(test_list)
print(type(test_list))
Output:
first second third fourth <class 'str'> Process finished with exit code 0
I also tried removing the spaces and using commaas instead. Then I used "," in split. It did not change anything.
Quote:first second third fourth
You need to add the argument for an empty space
.split(' ')
Ok, I tried that. Console showed the same output as before unfortunately. So there must be another issue.

I should also note when I used the comma seperator, I did not forget to include the ','
looks to me that test_list is a dictionary, not a list
no, I thank that's not true, but just the same:
do a favor, and after line 6, add
print(test_list)
and show results here.
Done, here is the output:

Output:
first second third fourth first second third fourth <class 'str'> Process finished with exit code 0
the only thing I can think of is that it's tab separated, not space.
you can try:
test_list.split('\t')
(Feb-22-2018, 12:51 AM)liquidsnake Wrote: [ -> ]I should also note when I used the comma seperator, I did not forget to include the ','
Your output is not showing it as comma separated but space separated.

What does this output?
Quote:
fb_user_data['users']['K2'].value
.split does not change the str in place so the next line does nothing
Quote:
test_list.split()

You need to do
test_list = test_list.split(' ')
As noted in the first post, the code showed space separation, but I also tried comma separation.

The output is the cell value of K2: first second third fourth

I think your 3rd comment may be the solution. I'll try assigning a new variable and printing that

Update: Bingo, the variable assignment captured the desired transformation:

Output:
['first', 'second', 'third', 'fourth'] <class 'list'>
That means you were changing it but not doing anything with the result...and using the unmodified one to print/do stuff