I have been working with beautiful soup to extract data from website APIs for use in a fan site I am building.
I have extracted the data into text files however I am having trouble formatting it.
Charles Dance
Lord Tywin Lannister (S 02+)
Natalie Dormer
Queen Margaery Tyrell (S 02+)
Harry Lloyd
Viserys Targaryen (S 01)
Mark Addy
King Robert Baratheon (S 01)
Alfie Allen
Theon Greyjoy
Sean Bean
Lord Eddard Stark (S 01)
I have several text files like this for shows.
I would like to have both the actor and character on the same line separated with commas for input into a database later.
Charles Dance , Lord Tywin Lannister (S 02+)
Natalie Dormer , Queen Margaery Tyrell (S 02+)
Harry Lloyd , Viserys Targaryen (S 01)
Mark Addy , King Robert Baratheon (S 01)
Alfie Allen , Theon Greyjoy
Sean Bean , Lord Eddard Stark (S 01)
If anyone could provide any assistance or pointers it would be greatly appreciated.
Solved:
Many thanks to Tdelaney and wnnmaw . you da real MVPs
def readline(fp):
#Read a line from a file, strip new line and raise Indexerror
#on end of file
line = fp.readline()
if not line:
raise IndexError()
return line.strip()
with open('Casts/GOTcast.txt') as in_file, open('GOTcastFIXED.txt', 'w') as out_file:
try:
while True:
out_file.write("%s, %s\n" % (readline(in_file), readline(in_file)))
except IndexError:
pass
解决方案
This ought to work:
>>> lst = ["a", "A", "b", "B", "c", "C"]
>>> lines = [" , ".join(tup) for tup in zip(lst[::2], lst[1::2])]
>>> lines
['a , A', 'b , B', 'c , C']
You can read your text file like this:
with open("yourfile.txt", 'r') as f:
lines = f.readlines()