Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a multi-word string into two variables

Tags:

python

I have a document that holds login details as "username1, password1, username2, password2 etc." Is there a way of splitting them into two arrays that hold just usernames and just passwords?

I tried doing: usernames, passwords = login.split(",") but that's just a ValueError.

Sorry if it's something so obvious!

Update:

login = "username1, password1, username2, password2"

like image 239
Jak Foster Avatar asked Mar 05 '23 02:03

Jak Foster


1 Answers

you are getting ValueError because login.split() returns a list with more than two elements.
If your data are formatted as username, password you can split and then slice the list:

login = "username1, password1, username2, password2"
data = login.split(",")


usernames = data[::2]
passwords = data[1::2]
like image 104
Lante Dellarovere Avatar answered Mar 09 '23 14:03

Lante Dellarovere