Python Forum
.split seemingly gets ignored
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.split seemingly gets ignored
#1
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.
Reply
#2
Quote:first second third fourth
You need to add the argument for an empty space
.split(' ')
Recommended Tutorials:
Reply
#3
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 ','
Reply
#4
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.
Reply
#5
Done, here is the output:

Output:
first second third fourth first second third fourth <class 'str'> Process finished with exit code 0
Reply
#6
the only thing I can think of is that it's tab separated, not space.
you can try:
test_list.split('\t')
Reply
#7
(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(' ')
Recommended Tutorials:
Reply
#8
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'>
Reply
#9
That means you were changing it but not doing anything with the result...and using the unmodified one to print/do stuff
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  why does VS want to install seemingly unrelated products? db042190 3 738 Jun-12-2023, 02:47 PM
Last Post: deanhystad
  Seemingly simple loop Weber585 7 3,643 Mar-21-2021, 07:19 PM
Last Post: jefsummers
  Seemingly unstable GPIO output while executing from RetroPie LouF 6 4,054 Feb-19-2021, 06:29 AM
Last Post: LouF

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020