也许我错过了一些复杂性,但假设文件完全按照您的陈述布置,您可以使用普通线条阅读方法。在Python中,文件读取在2和3版本中非常相似。 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。
你期待什么?你在找人为你编码吗?如果是这样,你应该聘请一名程序员。另一方面,如果你有一些你写的代码不符合你的要求,请发布它。我们可能会提供一些有用的提示。 –
好点。我只是在寻找正确的方向。比如,查看这种类型的动作,使用这种语言等。我真的不知道从哪里开始。 – echo
您使用的是Linux机器吗? – user454038