Python Forum
.split seemingly gets ignored - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: .split seemingly gets ignored (/thread-8470.html)



.split seemingly gets ignored - liquidsnake - Feb-21-2018

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.


RE: .split seemingly gets ignored - metulburr - Feb-21-2018

Quote:first second third fourth
You need to add the argument for an empty space
.split(' ')


RE: .split seemingly gets ignored - liquidsnake - Feb-22-2018

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 ','


RE: .split seemingly gets ignored - Larz60+ - Feb-22-2018

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.


RE: .split seemingly gets ignored - liquidsnake - Feb-22-2018

Done, here is the output:

Output:
first second third fourth first second third fourth <class 'str'> Process finished with exit code 0



RE: .split seemingly gets ignored - Larz60+ - Feb-22-2018

the only thing I can think of is that it's tab separated, not space.
you can try:
test_list.split('\t')



RE: .split seemingly gets ignored - metulburr - Feb-22-2018

(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(' ')



RE: .split seemingly gets ignored - liquidsnake - Feb-22-2018

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'>



RE: .split seemingly gets ignored - metulburr - Feb-22-2018

That means you were changing it but not doing anything with the result...and using the unmodified one to print/do stuff