Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo

Có một số câu hỏi tương tự với điều này trên StackoverFlow nhưng không ai làm chính xác những gì tôi muốn và những nỗ lực khác nhau của tôi dường như thất bại.

Tôi có một danh sách các URL, một số có dấu vết dấu vết, những người khác thì không ... Tôi muốn kiểm tra chúng và thêm một dấu gạch chéo cho những người không.

url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

for url in url_list:
    if url[len(url)-1] != "/":
        url = url + "/"
    else:
        url = url

print url_list

Trả về cùng một danh sách (hai URL cuối cùng vẫn không có dấu vết dấu vết)

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

Tại sao nó không hoạt động? Bất kỳ ý tưởng?

cảm ơn :)

Đã hỏi ngày 8 tháng 6 năm 2017 lúc 13:23Jun 8, 2017 at 13:23

Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo

the_t_test_1the_t_test_1the_t_test_1

1.1231 Huy hiệu vàng12 Huy hiệu bạc28 Huy hiệu đồng1 gold badge12 silver badges28 bronze badges

1

Lý do tại sao các thay đổi của bạn không hoạt động là khi bạn nói

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
8, điều này không chỉnh sửa
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
9 tại chỗ trong danh sách. Nếu bạn muốn làm theo cách này, bạn có thể tốt hơn khi sử dụng
for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
0:

for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list

Chúng tôi có thể gọn gàng hơn điều này bằng cách thay đổi

for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
1 thành
for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
2, trong đó đề cập đến nhân vật cuối cùng.

Nhưng trong khi chúng ta ở đó, tại sao không sử dụng

for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
3 và hiểu biết danh sách?

url_list = [u if u.endswith('/') else u + '/' for u in url_list]

Đã trả lời ngày 8 tháng 6 năm 2017 lúc 13:27Jun 8, 2017 at 13:27

asongtoruinasongtoruinasongtoruin

9.4663 huy hiệu vàng36 Huy hiệu bạc45 Huy hiệu đồng3 gold badges36 silver badges45 bronze badges

1

Bạn không thay đổi url_list. Để giữ cấu trúc ban đầu của mã của bạn, bạn có thể thử điều này:

url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

new_urls = []
for url in url_list:
    if url[len(url)-1] != "/":
        new_urls.append(url + "/")
    else:
        new_urls.append(url)
url_list = new_urls

print url_list

Bạn cũng có thể sử dụng .endswith () như được đề xuất trong một câu trả lời khác:

url_list = ['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']

new_urls = []
for url in url_list:
    if url.endswith('/'):
        new_urls.append(url)
    else:
        new_urls.append(url + "/")
url_list = new_urls

print url_list

Đã trả lời ngày 8 tháng 6 năm 2017 lúc 13:28Jun 8, 2017 at 13:28

Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo

Biến

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
9 của bạn được phân lập từ
for i, url in enumerate(url_list):
    if url[len(url)-1] != "/":
        url_list[i] = url + "/"

print url_list
5. Bạn có thể thử điều này:

for i, url in enumerate(url_list):
    if url.endswith("/"):
        url_list[i] = url + "/"
print(url_list)

Đã trả lời ngày 8 tháng 6 năm 2017 lúc 13:31Jun 8, 2017 at 13:31

Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo

Nói với Ktell ktell k

5952 Huy hiệu vàng6 Huy hiệu bạc18 Huy hiệu đồng2 gold badges6 silver badges18 bronze badges

Ngày: 2015-03-19 07:11

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
72014-08-26 17:58 by demian.brecht, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Lịch sử
NgàyNgười sử dụngHoạt độngArgs
2022-04-11 & NBSP; 14: 58: 07 quản trị viên, 2014-08-26 18:00
bộ GitHub: 66474, 2014-09-18 14:50 2015-04-15 & NBSP; 23: 11: 15
Berker.peksag
MSG225923 - (Xem)Tác giả: Demian Brecht (Demian.brecht) **
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
Ngày: 2014-08-26 17:58
Reported by Stefan Behnel in issue22118:

I'm now getting duplicated slashes in URLs, e.g.:

https://new//foo.html
http://my.little.server/url//logo.gif

In both cases, the base URL that gets joined with the postfix had a trailing slash, e.g.

"http://my.little.server/url/" + "logo.gif" -> "http://my.little.server/url//logo.gif"
MSG226164 - (Xem)Tác giả: Antoine Pitrou (Pitrou) **
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
Ngày: 2014-08-31 10:17
________số 8
MSG226168 - (Xem)Tác giả: Stefan Behnel (Scoder) **
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
Ngày: 2014-08-31 10:25
Were the tests in

http://bugs.python.org/file32591/urischemes.py

merged yet, that Nick Coghlan mentioned in http://bugs.python.org/issue22118#msg225662 ?
MSG226240 - (Xem)Tác giả: Antoine Pitrou (Pitrou) **
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
Ngày: 2014-08-31 10:17
________số 8
MSG226168 - (Xem)Tác giả: Demian Brecht (Demian.brecht) **
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
Tác giả: Stefan Behnel (Scoder) *
Ngày: 2014-08-31 10:25
Were the tests in

http://bugs.python.org/file32591/urischemes.py

merged yet, that Nick Coghlan mentioned in http://bugs.python.org/issue22118#msg225662 ?
Tác giả: Demian Brecht (Demian.brecht) **
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
MSG226240 - (Xem)
Ngày: 2014-09-01 17:52
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
0
MSG226249 - (Xem)*
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
Ngày: 2014-09-01 22:41
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
1
MSG227048 - (Xem)Ngày: 2014-09-18 14:50
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
2
MSG227086 - (Xem)
Tác giả: Senthil Kumaran (Orsenthil) * MSG226249 - (Xem)*
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
Ngày: 2014-09-01 22:41
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
1
MSG227048 - (Xem)Tác giả: Demian Brecht (Demian.brecht) **
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
Ngày: 2014-09-18 14:50
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
2
MSG227086 - (Xem)Tác giả: Senthil Kumaran (Orsenthil) **
Hướng dẫn python urljoin trailing slash - python urljoin dấu gạch chéo
Ngày: 2014-09-19 09:14
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
3
MSG227254 - (Xem)
Tác giả: Roundup Robot (Python-Dev)Ngày: 2014-09-22 07:49
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
4
MSG227255 - (Xem)
Ngày: 2014-09-22 07:50
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
5
MSG227273 - (Xem)Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
messages: + msg238494
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
assignee: orsenthil
resolution: fixed
messages: + msg227255
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
messages: + msg227254
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6

MSG238494 - (Xem) MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem)Tác giả: Martin Panter (Martin.panter) *Ngày: 2015-03-19 07:11
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
7
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13

['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6

MSG238494 - (Xem) MSG238494 - (Xem) MSG227273 - (Xem)Ngày: 2014-09-22 14:13
keywords: + patch
['http://google.com/somedirectory/', 'http://google.com/someotherdirectory/', 'http://google.com/anotherdirectory', 'http://google.com/yetanotherdirectory']
6
MSG238494 - (Xem)Tác giả: Martin Panter (Martin.panter) *