How to split each line from file to it's own s

2019-09-22 00:48发布

I have a file with a bunch of information with this following format:

    <name>New York Jets</name>

I am trying to get each line of the file into it's own string. For example, I want this line to say "This is the roster for the New York Jets." I have this so far, but it has "This is the roster for the" for every single line. I think I have to use-

    inputString.split('\n')

But I'm not sure where to put it in at. This is what I have so far.

    def summarizeData(filename):
        with open(filename,"r") as fo:
             for rec in fo:
                 name=rec.split('>')[1].split('<')[0]
                 print("Here is the roster for the %s." % (name))

and I call summarizeData("NewYorkJets.txt"). So basically I am trying to split each line from the file to get it in it's own string

标签: python file
1条回答
女痞
2楼-- · 2019-09-22 01:16
from xml.dom import minidom

xmldoc = minidom.parse('filename.txt')

itemlist = xmldoc.getElementsByTagName('item')

for s in itemlist: print(s.attributes['name'].value)

you can read a file having tags like this, and retrieve the values.

查看更多
登录 后发表回答