newbie - HTML character codes
Hi sorry if I'm asking something very obvious but I'm stumped. I have a text that looks like this: Sentence 401 4.00pm — We set off again; this time via Tony's home to collect a variety of possessions, finally arriving at hospital no.3. Sentence 402 4.55pm — Tony is ushered into a side ward with three doctors and I stay outside with Mum. And I want the HTML char codes to turn into their equivalent plain text. I've looked at the newsgroup archives, the cookbook, the web in general and can't manage to sort it out. I thought doing something like this - file = open('filename', 'r') ofile = open('otherfile', 'w') done = 0 while not done: line = file.readline() if 'THE END' in line: done = 1 elif '—' in line: line.replace('—', '--') ofile.write(line) else: ofile.write(line) would do it but it isn'twhere am I going wrong? many thanks rachele -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie - HTML character codes
thank you both - in the end I used recode, which I wasn't aware of. Fredrik, I had come across your script while googling for solutions, but failed to make it work On Dec 13, 2:21 pm, "Fredrik Lundh" <[EMAIL PROTECTED]> wrote: > "ardief" wrote: > > sorry if I'm asking something very obvious but I'm stumped. I have a > > text that looks like this: > > > Sentence 401 > > 4.00pm — We set off again; this time via Tony's home to collect > > a variety of possessions, finally arriving at hospital no.3. > > Sentence 402 > > 4.55pm — Tony is ushered into a side ward with three doctors and > > I stay outside with Mum. > > > And I want the HTML char codes to turn into their equivalent plain > > text. I've looked at the newsgroup archives, the cookbook, the web in > > general and can't manage to sort it out. > > file = open('filename', 'r') > > ofile = open('otherfile', 'w') > > > done = 0 > > > while not done: > >line = file.readline() > >if 'THE END' in line: > >done = 1 > >elif '—' in line: > >line.replace('—', '--')this returns a new line; it doesn't > > update the line in place. > > >ofile.write(line) > >else: > >ofile.write(line)for a more general solution to the actual replace > > problem, see: > >http://effbot.org/zone/re-sub.htm#unescape-html > > you may also want to lookup the "fileinput" module in the library reference > manual. > > -- http://mail.python.org/mailman/listinfo/python-list
newbie/ merging lists of lists with items in common
Hi everyone Here is my problem: I have a list that looks like this - [['a', '13'], ['a', '3'], ['b', '6'], ['c', '12'], ['c', '15'], ['c', '4'], ['d', '2'], ['e', '11'], ['e', '5'], ['e', '16'], ['e', '7']] and I would like to end up with something like this, i.e. with the only one list per letter: [['a', ['13' '3']], ['b', '6'], ['c', ['12', '15', '4']], ['d', '2'], ['e', ['11', '5', '16', '7']]] I have the feeling it's trivial, and I've scoured the group archives - sets might be a possibility, but I'm not sure how to operate on a list of lists with sets. This function also gives me what I want, more or less, but I don't know how to make it run until it's covered all the possibilities, if that makes sense... def sigh(list): for a in list: i = list.index(a) if a != list[-1]:##if a is not the last one, i.e. there is a next one n = alist[i+1] if a[0] == n[0]: a.append(n[1:]) del alist[i+1] Sorry about the lengthy message and thanks for your suggestions - I'm trying to learn... -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie/ merging lists of lists with items in common
On Feb 2, 1:55 pm, "ardief" <[EMAIL PROTECTED]> wrote: > Hi everyone > Here is my problem: > I have a list that looks like this - > [['a', '13'], ['a', '3'], ['b', '6'], ['c', '12'], ['c', '15'], ['c', > '4'], ['d', '2'], ['e', '11'], ['e', '5'], ['e', '16'], ['e', '7']] > > and I would like to end up with something like this, i.e. with the > only one list per letter: > > [['a', ['13' '3']], ['b', '6'], ['c', ['12', '15', '4']], ['d', '2'], > ['e', ['11', '5', '16', '7']]] thanks to everyone for the help, and the speed of it! It's really useful and I will spend some time working on understanding the code you posted. I'd be so lost without this group... r -- http://mail.python.org/mailman/listinfo/python-list