开发者

parse variables file in Python

I wish to send Python script a definition file (with key and value pairs) which it should translate into an htm开发者_StackOverflowl table using templates.

For example: say that the definitions file def.txt contain the following lines:

build number: 5513
build date: 12/09/2011

I want it to be able to read it easily (iterate on each line) and create a data table. something like:

<tbl>
   <tr> <td> build number </td> <td> 5515 </td> </tr>
   <tr> <td> build date </td> <td> 12/09/2011</td> </tr>
</tbl>

I want it to work for N lines in definition file. I don't care about the definition file format...

Any suggestions?

Thanks.

Or.


>>> l = [i.split(": ") for i in open("def.txt")] #read and parse the input
>>> l #here is what we got
[['build number', '5513'], ['build date', '12/09/2011']]
>>> with open("out.txt", "w") as f: #open the out file
...    f.write("<tbl>\n") 
...    for k, v in l: #iterate and write
...         f.write("<tr><td>{}</td><td>{}</td></tr>\n".format(k, v)) 
...    f.write("</tbl>\n")


config module

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜