This is what I did. I opened it in Notepad, and searched for "PC" and "Mac" and found many lines that I want to copy. The problem is, I want to copy the line before the name "PC" and after the name "Mac". Example:
I want to copy this line:
. blutenoccor.weebly.com/kwestionariusz-dla-nierezydenta-pdf-freel.html. reivalb dd23f8915e
and paste it after this line:
zisis:~> cp *.pdf.
I know I can do this with a for loop but this is my first time to work with webpages and this is driving me crazy. I don't know where to start.
Any ideas?
A:
Try this:
import os
import re
r = re.compile(".*Mac.*")
with open('sample.txt', 'r') as f:
for line in f:
if r.match(line):
x = line.split()[1]
print(x)
Output:
zisis:~> python test.py
blutenoccor.weebly.com/kwestionariusz-dla-nierezydenta-pdf-freel.html
reivalb dd23f8915e
It'll look for lines with Mac somewhere in it. The re.compile(r) searches for the text "Mac" in the line. Once it finds it, it can return just the text it found. The re.match(r) matches that text and returns a match. The line split splits the line into tokens and returns a list of them. The [1] index will return the 2nd item in the list.
A:
import re
r = re.compile(".*Mac.*")
for line in open('sample.txt', 'r'):
if r.match(line):
print line
You may want to look up the documentation on regular expressions, which is where the regex stuff is. be359ba680
Related links:
Commentaires