-5

I am new to Python. I have input in this format

Harry is American Harry is American Harry is American Harry is American
Harry is American Harry is American Harry is American Harry is American
Harry is American Harry is American Harry is American Harry is American

And my output should should like this

Harry, 1
is, 1
American, 1
Harry, 1
is, 1
American, 1
....

Can you please tell me the solution...

I am trying with below code(my code was not even closer to the solution).

import sys
import re
inp = open('menu.bak', "r")
outp = open('word.txt',"w")
for line in inp:
  counts = len(re.findall(r'\w+', line))
  new = ''.join(line)
  print(new[0])
  print(new[1])
inp.close()
outp.close()

Thanks, Gokul S

4

1 回答 1

1
import re
inp = open('menu.bak', "r")
outp = open('word.txt',"w")
for line in inp:
  for word in line.split():
    outp.write(str(word) + "," + "1" + '\n')
inp.close()
outp.close()
于 2013-09-23T05:57:04.437 回答