2016-02-25 174 views
-2

我有一个txt文件,其中包含电视台名称列表,后面跟着它们的位置,我需要用HTML标记包装。该文本文件遵循以下格式:解析文本文件为HTML标记

Prime TV 
Orange, Australia 

Prime TV 
Tamworth, Australia 

Prime TV 
Wagga Wagga, Australia 

前行的名字,第二行是位置,第三行是空白(分隔符)。这些只有150多个。

我期待下面的标记来包装这样的:

<div class="CL_item"> 
     <p class="CL_title">$StationName</p> 
     <p class="CL_sub">$StationLoc</p> 
    </div> 

我只是不知道如何做到这一点。我研究过一些PHP(http://html.net/tutorials/php/lesson15.php),但似乎并不能区分顶部/底部线。我没有和一个PHP解决方案结婚。我只是在寻找一个启动点,我不知道从哪里开始。蟒蛇? bash grep?

+0

你期待什么?你在找人为你编码吗?如果是这样,你应该聘请一名程序员。另一方面,如果你有一些你写的代码不符合你的要求,请发布它。我们可能会提供一些有用的提示。 –

+0

好点。我只是在寻找正确的方向。比如,查看这种类型的动作,使用这种语言等。我真的不知道从哪里开始。 – echo

+0

您使用的是Linux机器吗? – user454038

回答

-1

也许我错过了一些复杂性,但假设文件完全按照您的陈述布置,您可以使用普通线条阅读方法。在Python中,文件读取在23版本中非常相似。 readline方法对你的情况特别有用。

因此,例如,你可以遍历文件一次读取三行,直到再结,像这样:

def get_stations(file_name): 
    # open the file for reading 
    with open(file_name) as f: 
     # for every station name & location... 
     while True: 
      # get the name and strip out the newline character 
      name = f.readline().strip() 
      # if the name is empty then we've probably reached 
      # the end of the file 
      if name == '': break 
      # get the location in the same way we got the name 
      location = f.readline().strip() 
      # yield allows us to use this function in a for...in loop 
      yield name, location 

      # skip the blank line between stations 
      f.readline() 

你会使用这个功能在一个for ... in循环,可能使用一个简单的模板来输出数据。

# Our template string. This could also be read in from a file. 
# Anything in curly brackets is a {variable} name. 
template = ''' 
<div class="CL_item"> 
    <p class="CL_title">{station_name}</p> 
    <p class="CL_sub">{station_location}</p> 
</div>''' 

# create and open output.html for writing 
with open('output.html', 'w') as f: 
    # loop through every station in the input.txt file 
    for name, location in get_stations('input.txt'): 
     # add the stations name and location using the template's format method 
     station_html = template.format(
      station_name = name, 
      station_location = location 
     ) 
     f.write(station_html) 

请注意,我没有真正尝试过这样,所以它可能需要适应您的特定文件,如果它包含任何怪癖。另外你应该知道格式函数不会从字符串中隐藏尖括号('<'和'>'),所以如果这是一个问题,你可能想查找如何在Python中转义html。