Forums

Splitting String Not Working

I am trying to split a string at the "-" using str.split("-") where str is the string and it does not seem to be functioning. Weird thing is that it is working on my local server, but not in python anywhere. My thought is that the .split() function may not work in python anywhere. Has anyone experienced this?

Could you post a short example of how you're trying to use 'split' please?

+1 to what @jgmdavies says. The split function should definitely work exactly the same on PythonAnywhere as it does on your local machine. But there may be something weird and non-obvious going on, and seeing the code would help us diagnose it.

Apologies for the delay -- linked below:

for key, value in request.POST.items():
        if "choice" in key:
            game_id = int(key.split("-")[1])
            type = value.split("-")[0]
            team_id = int(value.split("-")[1])

The code is splitting on my local machine but in python anywhere I am getting an error that game_id is not an int because it is not splitting

My guess is that the string you think you're splitting and the string that you are splitting are different. Check the value of key before you split. You'll probably find that the value is different to what you expect.

my split('-') is not workig on pythonanywhere

Can you give us more details on the exact error that you are seeing? Here are some examples of details you could provide so we can better help you diagnose your problem:

  1. What is the full error message that you are seeing and can you give us a copy of the error stacktrace? Tip: If you were running code in the console, you should be able to see the traceback on your screen when your code errors. If you are seeing an error for your website, you will likely find it in your error logs. For a scheduled task, you can look at the task logs.

  2. Where on PythonAnywhere did you see this error, and what did you do to generate that error? eg: a step-by-step of you ran command1, then command2, then command3 fails all from within a PythonAnywhere console; or say first you created a webapp from our web tab, and then you setup a virtualenv from the console, and now reloading your website fails.

  3. Can you reproduce the same error by following the same process again, or is it an intermittent error? Is there a particular recent code change that is causing this error, and does the error go away if you back out the code change and reload/rerun?

  4. Do you have any thoughts on what the issue may be, or is there anything peculiar about your setup that we may want to take into consideraiton as possible causes? Do you have any insights/guesses from your attempts to debug the problem so far?

This is the error message:

File "/home/roilait2009/mysite/Resources/routes/db_users.py", line 726, in udpate_gp_state 2018-10-26 01:24:20,629: _date = gp.d_date.split("-")

_date = gp.d_date.split("-") year, month, day = _date[0], _date[1], _date[2]

all is working fine in local, but it is not working on pythonanywhere, I have on many page with split()

so have you tried printing out what gp.d_date is?

Thanks,

Evry things is working in local gp.dates = 2018-10-27 and on my computer gp.dates.split('-') gives me 2018, 10, 27.

but when i go on pythonanywhere i got

File "/home/roilait2009/mysite/Resources/routes/db_users.py", line 726, in udpate_gp_state 2018-10-26 01:24:20,629: _date = gp.d_date.split("-")

brendanbernstein did you fix your problem withe split("-"), if yes, I would like to ask you how did you fix it. Thanks

also what is the full error message?