0

I am trying to split a url string in Python 3.

The code is like this.

url = 'https://www.jstage.jst.go.jp/browse/mesj/58/1-2/_contents/-char/en' url = url.replace('https://www.jstage.jst.go.jp/browse/', '') j, v, n, _ = url.split('/') print(j, v, n) 

I want to extract journal name, volume and number from a url.

But there is an error like this.

user@users-MacBook-Pro-5 ~ % /usr/bin/python3 /Users/user/Downloads/test.py Traceback (most recent call last): File "/Users/user/Downloads/test.py", line 5, in <module> j, v, n, _ = url.split('/') ValueError: too many values to unpack (expected 4) 

Any suggestion?

1
  • There are 6 pieces in that split path, which you're trying to assign to only 4 variables… Commented May 27, 2020 at 7:41

1 Answer 1

2

Because url (mesj/58/1-2/_contents/-char/en) has five '/', you should use j, v, n, _, __ = url.split('/').

But, the following code is more useful.

j, v, n, *_ = url.split('/') 

The first, second, third one are assigned to j, v, n, and the others are assigned to _.

Sign up to request clarification or add additional context in comments.

1 Comment

or j, v, n = url.split('/')[0:3]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.