1
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi mới đến Python, học những điều cơ bản.
Truy vấn của tôi: Tôi có nhiều trang được truy cập dưới dạng yêu cầu từ tệp nhật ký như bên dưới, I have multiple pages accessed as a request from a log file like the below,
"GET /img/home/search-user-ico.jpg HTTP/1.1"
"GET /SpellCheck/am.tlx HTTP/1.1"
"GET /img/plan-comp-nav.jpg HTTP/1.1"
"GET /ie6.css HTTP/1.1"
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1"
"GET /SpellCheck/am100k2.clx HTTP/1.1"
"GET /SpellCheck/am.tlx HTTP/1.1"
Câu hỏi của tôi là tôi chỉ muốn phần tệp từ trang, ví dụ, chúng ta hãy coi
parts = line.split[" "]
0 là một trang sau đó từ những điều trên tôi muốn chia parts = line.split[" "]
1, parts = line.split[" "]
2.parts = line.split[" "]
1, parts = line.split[" "]
2.Vì vậy, các chuyên gia xin vui lòng giúp tôi viết kịch bản Python để chia ra.
hỏi ngày 18 tháng 4 năm 2011 lúc 8:53Apr 18, 2011 at 8:53
2
Giả sử rằng bạn không có khoảng trống trong tên tệp và bạn không muốn "HTTP" ở cuối.
Bạn có thể chia dòng theo không gian.
parts = line.split[" "]
và sau đó sử dụng mô -đun
parts = line.split[" "]
3 để lấy tên tệp từ đường dẫn.filename = os.path.basename[parts[1]]
Ví dụ.
>>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
>>> parts = line.split[" "]
>>> parts[1]
'/img/home/search-user-ico.jpg'
>>> os.path.basename[parts[1]]
'search-user-ico.jpg'
Đã trả lời ngày 18 tháng 4 năm 2011 lúc 9:03Apr 18, 2011 at 9:03
Stephen Paulgerstephen PaulgerStephen Paulger
5.0833 Huy hiệu vàng25 Huy hiệu bạc44 Huy hiệu đồng3 gold badges25 silver badges44 bronze badges
2
data = [
"GET /img/home/search-user-ico.jpg HTTP/1.1",
"GET /SpellCheck/am.tlx HTTP/1.1",
"GET /img/plan-comp-nav.jpg HTTP/1.1" ,
"GET /ie6.css HTTP/1.1",
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
"GET /SpellCheck/am100k2.clx HTTP/1.1" ,
"GET /SpellCheck/am.tlx HTTP/1.1"
]
for url in data:
print url.split[' '][1].split['/'][-2]
Đã trả lời ngày 18 tháng 4 năm 2011 lúc 9:33Apr 18, 2011 at 9:33
1
data = [
"GET /img/home/search-user-ico.jpg HTTP/1.1",
"GET /SpellCheck/am.tlx HTTP/1.1",
"GET /img/plan-comp-nav.jpg HTTP/1.1" ,
"GET /ie6.css HTTP/1.1",
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
"GET /SpellCheck/am100k2.clx HTTP/1.1" ,
"GET /SpellCheck/am.tlx HTTP/1.1"
]
for url in data:
print url.split[' '][1].split['/'][-1]
Đã trả lời ngày 18 tháng 4 năm 2011 lúc 9:05Apr 18, 2011 at 9:05
AchimachimAchim
15.1K15 Huy hiệu vàng75 Huy hiệu bạc139 Huy hiệu đồng15 gold badges75 silver badges139 bronze badges
Nếu định dạng của các liên kết của bạn là tương tự. Một giải pháp khác sẽ là:
request = "GET /img/home/search-user-ico.jpg HTTP/1.1"
parts = request.split["/"]
parts[-2] //returns search-user-ico.jpg HTTP
Đã trả lời ngày 18 tháng 4 năm 2011 lúc 9:11Apr 18, 2011 at 9:11
Omerkirkomerkirkomerkirk
2.5371 Huy hiệu vàng17 Huy hiệu bạc9 Huy hiệu đồng1 gold badge17 silver badges9 bronze badges
Làm thế nào để bạn chia một chuỗi theo URL trong Python?
Phương pháp số 1: Sử dụng Split [] 'và trả về phần đầu tiên của Split cho kết quả.
Làm thế nào để bạn cắt một url trong python?
Làm thế nào để bạn chia một chuỗi theo URL trong Python?
Phương pháp số 1: Sử dụng Split [] 'và trả về phần đầu tiên của Split cho kết quả.
Làm thế nào để bạn cắt một url trong python?
Phương thức phụ [] để xóa URL khỏi văn bản, ví dụ: Kết quả = Re. sub [r'http \ s+',' ', my_string]. Ở đó. Phương thức Sub [] sẽ xóa bất kỳ URL nào khỏi chuỗi bằng cách thay thế chúng bằng các chuỗi trống.
Method #1 : Using split[]
This is one of the way in which we can solve this problem. We split by ‘?’ and return the first part of split for result.
Python3
parts = line.split[" "]
4parts = line.split[" "]
5 parts = line.split[" "]
6parts = line.split[" "]
7parts = line.split[" "]
8parts = line.split[" "]
9 filename = os.path.basename[parts[1]]
0 filename = os.path.basename[parts[1]]
1filename = os.path.basename[parts[1]]
2filename = os.path.basename[parts[1]]
3parts = line.split[" "]
5 filename = os.path.basename[parts[1]]
5filename = os.path.basename[parts[1]]
6filename = os.path.basename[parts[1]]
7filename = os.path.basename[parts[1]]
8filename = os.path.basename[parts[1]]
9parts = line.split[" "]
7parts = line.split[" "]
8>>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
>>> parts = line.split[" "]
>>> parts[1]
'/img/home/search-user-ico.jpg'
>>> os.path.basename[parts[1]]
'search-user-ico.jpg'
2 filename = os.path.basename[parts[1]]
0 >>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
>>> parts = line.split[" "]
>>> parts[1]
'/img/home/search-user-ico.jpg'
>>> os.path.basename[parts[1]]
'search-user-ico.jpg'
4Đầu ra: & nbsp;
The original string is : www.geeksforgeeks.org?is=best The base URL is : www.geeksforgeeks.org
Độ phức tạp về thời gian: O [n] -> [Chức năng phân chia]O[n] -> [split function]
Không gian phụ trợ: O [n]O[n]
& nbsp; Phương thức #2: Sử dụng rfind [] & nbsp; Đây là một cách khác để chúng ta cần thực hiện nhiệm vụ này. Trong đó, chúng tôi tìm thấy sự xuất hiện đầu tiên của ‘? Từ bên phải và cắt chuỗi. & Nbsp;
Method #2 : Using rfind[]
This is another way in which we need to perform this task. In this, we find the first occurrence of ‘?’ from right and slice the string.
Python3
parts = line.split[" "]
4parts = line.split[" "]
5 parts = line.split[" "]
6parts = line.split[" "]
7parts = line.split[" "]
8parts = line.split[" "]
9 filename = os.path.basename[parts[1]]
0 filename = os.path.basename[parts[1]]
1filename = os.path.basename[parts[1]]
2filename = os.path.basename[parts[1]]
3parts = line.split[" "]
5 data = [
"GET /img/home/search-user-ico.jpg HTTP/1.1",
"GET /SpellCheck/am.tlx HTTP/1.1",
"GET /img/plan-comp-nav.jpg HTTP/1.1" ,
"GET /ie6.css HTTP/1.1",
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
"GET /SpellCheck/am100k2.clx HTTP/1.1" ,
"GET /SpellCheck/am.tlx HTTP/1.1"
]
for url in data:
print url.split[' '][1].split['/'][-2]
6filename = os.path.basename[parts[1]]
6data = [
"GET /img/home/search-user-ico.jpg HTTP/1.1",
"GET /SpellCheck/am.tlx HTTP/1.1",
"GET /img/plan-comp-nav.jpg HTTP/1.1" ,
"GET /ie6.css HTTP/1.1",
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
"GET /SpellCheck/am100k2.clx HTTP/1.1" ,
"GET /SpellCheck/am.tlx HTTP/1.1"
]
for url in data:
print url.split[' '][1].split['/'][-2]
8parts = line.split[" "]
7parts = line.split[" "]
8>>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
>>> parts = line.split[" "]
>>> parts[1]
'/img/home/search-user-ico.jpg'
>>> os.path.basename[parts[1]]
'search-user-ico.jpg'
2 filename = os.path.basename[parts[1]]
0 >>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
>>> parts = line.split[" "]
>>> parts[1]
'/img/home/search-user-ico.jpg'
>>> os.path.basename[parts[1]]
'search-user-ico.jpg'
4Đầu ra: & nbsp;
The original string is : www.geeksforgeeks.org?is=best The base URL is : www.geeksforgeeks.org
Độ phức tạp về thời gian: O [n] -> [Chức năng phân chia]O[n]
Không gian phụ trợ: O [n]O[n]
& nbsp; Phương thức #2: Sử dụng rfind [] & nbsp; Đây là một cách khác để chúng ta cần thực hiện nhiệm vụ này. Trong đó, chúng tôi tìm thấy sự xuất hiện đầu tiên của ‘? Từ bên phải và cắt chuỗi. & Nbsp;Using index[].Finding index of ‘?’ and then used string slicing
Python3
parts = line.split[" "]
4parts = line.split[" "]
5 parts = line.split[" "]
6parts = line.split[" "]
7parts = line.split[" "]
8parts = line.split[" "]
9 filename = os.path.basename[parts[1]]
0 filename = os.path.basename[parts[1]]
1filename = os.path.basename[parts[1]]
2filename = os.path.basename[parts[1]]
3parts = line.split[" "]
5 data = [
"GET /img/home/search-user-ico.jpg HTTP/1.1",
"GET /SpellCheck/am.tlx HTTP/1.1",
"GET /img/plan-comp-nav.jpg HTTP/1.1" ,
"GET /ie6.css HTTP/1.1",
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
"GET /SpellCheck/am100k2.clx HTTP/1.1" ,
"GET /SpellCheck/am.tlx HTTP/1.1"
]
for url in data:
print url.split[' '][1].split['/'][-2]
6filename = os.path.basename[parts[1]]
6data = [
"GET /img/home/search-user-ico.jpg HTTP/1.1",
"GET /SpellCheck/am.tlx HTTP/1.1",
"GET /img/plan-comp-nav.jpg HTTP/1.1" ,
"GET /ie6.css HTTP/1.1",
"GET /img/portlet/portlet-content-bg.jpg HTTP/1.1",
"GET /SpellCheck/am100k2.clx HTTP/1.1" ,
"GET /SpellCheck/am.tlx HTTP/1.1"
]
for url in data:
print url.split[' '][1].split['/'][-2]
8parts = line.split[" "]
7parts = line.split[" "]
8>>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
>>> parts = line.split[" "]
>>> parts[1]
'/img/home/search-user-ico.jpg'
>>> os.path.basename[parts[1]]
'search-user-ico.jpg'
2 filename = os.path.basename[parts[1]]
0 >>> line = "GET /img/home/search-user-ico.jpg HTTP/1.1"
>>> parts = line.split[" "]
>>> parts[1]
'/img/home/search-user-ico.jpg'
>>> os.path.basename[parts[1]]
'search-user-ico.jpg'
4Đầu ra: & nbsp;
The original string is : www.geeksforgeeks.org?is = best The base URL is : www.geeksforgeeks.org
Độ phức tạp về thời gian: O [n] -> [Chức năng phân chia]O[n]
Không gian phụ trợ: O [n]O[n]