I have a.csv file with this content:
HEAD / HTTP/1.1
POST /app/something1/ HTTP/1.1
GET /app/something2/ HTTP/1.1
GET /app/something3/ HTTP/1.1
and can't find a way to remove everything after the first fw slash.
I've tried some regex but without such a knowledge it looks more difficult than going with python.
This is basic code that I've tried:
with open('log1.csv') as f:
for line in f:
f[:f.index("/")]
The expected result is this:
Head
POST
GET
GET
Can you please assist me on the right code portion?
You just need to read each line in, and split()
on '\'
and index the first item:
# Open the file in read more
# mode='r' by default if you don't specify
with open('log1.csv') as f:
# Read each line from the iterator
for line in f:
# Strip newlines
line = line.strip()
# Only process non-empty lines
if line:
# Split the line and take the first item
print(line.split('/')[0])
Output:
HEAD
POST
GET
GET
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.