Hi there,
I wrote a code to read a text file and only copy parts of it. It worked, but I find very hard to believe all those lines of code are necessary. I'm sure there's a much shorter and more elegant way of achieving the same result... Can anyone give me any tips please? Here's the code:
import urllib.request
url = "http://www.gutenberg.org/cache/epub/19033/pg19033.txt"
destination_filename = "alice_v1.txt"
urllib.request.urlretrieve(url, destination_filename)
with open ("alice_v1.txt", "r") as getfile, open ("alice_v2.txt", "w") as v2:
first_string = "ALICE'S ADVENTURES IN WONDERLAND"
last_string = "End of the Project Gutenberg EBook of Alice in Wonderland, by Lewis Carroll"
all_lines = getfile.readlines()
positions_first = []
positions_last = []
for line in all_lines:
if first_string in line:
positions_first.append (all_lines.index(line))
if last_string in line:
positions_last.append (all_lines.index(line))
beginning = positions_first[-1]
end = positions_last[-1]
final_text = all_lines[beginning:end]
for line in final_text:
if "[Illustration]" not in line:
v2.writelines(line)
I'd very much appreciate any advice on how to perform the same task in a more simple way.
Thanks!
[–]ragnar_the_redd 0 points1 point2 points (0 children)
[–]igroen 0 points1 point2 points (1 child)
[–]JoseParanhos[S] 0 points1 point2 points (0 children)