如何从csv文件中读取数据并将数据插入到html文件中

时间:2017-01-23 19:45:24

标签: javascript python html csv

我正在尝试创建一个Python脚本,它将(a)读取带有数据的csv文件,(b)获取该数据并将其插入到html字符串的特定标记中,(c)最后编写修改后的html字符串到html文件。

到目前为止,我有一个脚本从csv文件读入:

import csv

with open('data.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row

f.close()

我也有编写html文件的脚本:

import webbrowser

# open blank file
f = open('template.html', 'w')

message = """<style scoped="scoped" type="text/css">h1.page-title { display: none; }</style>

<p>&nbsp;</p>

<div class="row">
    <div class="col-xs-12 col-sm-8 col-md-9">
        <div class="twoblocks">
            <div class="blocks-holder grey">

                <div class="block-left w50 bkgimage" style="background-image: url('/images/common/not_pictured-wide.jpg');">
                    <div class="force-16x9">&nbsp;</div>
                </div>

                <div class="block-right w50 content" style="width: 50%;">
                <h1>Dr. Rosen Rosen</h1>
                <span class="superhead">Title, <a href="index.php?Itemid=XXXX">Department</a></span>
                </div>

            </div>
        </div>

        <h3>Biography</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam hendrerit mauris dictum metus porta, in consectetur dui aliquam. Nunc eu pharetra ipsum. Donec nec tincidunt dolor. Mauris dolor nisi, pellentesque quis tellus malesuada, placerat tristique dui. Phasellus at dui in nunc vestibulum accumsan. Aliquam erat volutpat. Duis non interdum ipsum, et tristique leo. Pellentesque condimentum, felis id placerat placerat, purus purus mattis urna, ac sodales ipsum ipsum ut risus. Aenean eget molestie ante, nec blandit magna. Integer efficitur fringilla odio sit amet sagittis. Sed ante turpis, pulvinar condimentum vehicula ac, tempus cursus ante. Donec eu scelerisque metus. Vestibulum facilisis tincidunt fermentum. Phasellus consequat odio in libero pharetra rutrum.</p>

        <hr />

        <h3>Education</h3>
        <ul class="list-unstyled list-spaced">
            <li>Ph.D., Field of Degree, University of Lorem Ipusm, 1990</li>
            <li>M.S., Field of Degree, University of Lorem Ipusm, 1990</li>
            <li>B.S., Field of Degree, University of Lorem Ipusm, 1990</li>
        </ul>

        <hr />

        <h3>Classes Taught</h3>
        <ul class="list-unstyled list-spaced">
            <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - First Class</a></li>
            <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Second Class</a></li>
            <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Third Class</a></li>
        </ul>

        <div id="classes" class="collapse">

            <ul class="list-unstyled list-spaced top-0">
                <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Fourth Class</a></li>
                <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Fifth Class</a></li>
                <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Sixth Class</a></li>
            </ul>

        </div>

        <button type="button" class="btn btn-default expand collapsed" data-toggle="collapse" data-target="#classes"></button>

        <hr />

        <h3>Affiliations</h3>
        <ul class="list-unstyled list-spaced">
            <li>Organization</li>
            <li>Organization</li>
            <li>Organization</li>
        </ul>

        <div id="affiliations" class="collapse">

            <ul class="list-unstyled list-spaced top-0">
                <li>Organization</li>
                <li>Organization</li>
                <li>Organization</li>
            </ul>

        </div>

        <button type="button" class="btn btn-default expand collapsed" data-toggle="collapse" data-target="#affiliations"></button>

        <hr />

        <h3>Publications</h3>
        <ul class="list-unstyled list-spaced">
            <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
            <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
            <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
        </ul>

        <div id="publications" class="collapse">

            <ul class="list-unstyled list-spaced top-0">
                <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
                <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
                <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
            </ul>

        </div>

        <button type="button" class="btn btn-default expand collapsed" data-toggle="collapse" data-target="#publications"></button>

    </div>

    <aside class="col-xs-12 col-sm-4 col-md-3">
        <div class="well">

            <h3>Contact</h3>
            <p>Address <br />Building Name <br />Room # <br />City, State, Zip <br /> <a href="/map"><i class="ico-map"></i>Map</a></p>
            <p><i class="ico-phone"></i> XXX.XXX.XXXX <br /> <a href="mailto:">name@example.com</a></p>
        </div>

        <h3>Academic Interests</h3>
        <ul class="list-unstyled list-spaced">
            <li>Area</li>
            <li>Another Area</li>
            <li>Area</li>
            <li>Another Area</li>
        </ul>

    </aside>

</div>"""

# write to file
f.write(message)

# close file
f.close()

# open in browser
filename = 'file:////Scripts/' + 'template.html'
webbrowser.open_new_tab(filename)

到目前为止,我可以从csv文件中读取信息,我也可以使用我正在使用的模板创建一个html文件。在我遇到困难的地方,是如何获取该csv信息并将该信息以标签中的特定顺序粘贴到html中。

例如,csv中的每一行都有填写html模板的信息。基本上,第一列包含<h1>Dr. Rosen Rosen</h1>的“名称”,下一列包含<span class="superhead">Title,的“标题”,依此类推至html。

这个脚本基本上允许我使用带有大量信息的csv并构建一个网页,这样我就不必不断地进入代码并手动输入它。我只是在弄清楚如何让这个脚本做我想要它做的事情,因为我对Python比较陌生。

感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

您需要将脚本合并到一个脚本中。对于csv文件中的每一行,您都会为该医生提取数据,然后使用字符串连接将其与巨大的html字符串拼接在一起。然后将该字符串写入输出文件。

以下是输出名称,标题和部门数据的示例。

但是,我必须同意@TankorSmash,这可能不是最佳解决方案。某种templating engine通常更容易使用。

import webbrowser
import csv

# open blank file
f = open('template.html', 'w')


with open('./data.csv', 'r') as c:
    reader = csv.reader(c)
    for row in reader:
        name = row[0]
        title = row[1]
        department = row[2]

        message = """<style scoped="scoped" type="text/css">h1.page-title { display: none; }</style>

<p>&nbsp;</p>

<div class="row">
    <div class="col-xs-12 col-sm-8 col-md-9">
        <div class="twoblocks">
            <div class="blocks-holder grey">

                <div class="block-left w50 bkgimage" style="background-image: url('/images/common/not_pictured-wide.jpg');">
                    <div class="force-16x9">&nbsp;</div>
                </div>

                <div class="block-right w50 content" style="width: 50%;">
                <h1>""" + name + """</h1>
                <span class="superhead">""" + title + """, <a href="index.php?Itemid=XXXX">""" + department + """</a></span>
                </div>

            </div>
        </div>

        <h3>Biography</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam hendrerit mauris dictum metus porta, in consectetur dui aliquam. Nunc eu pharetra ipsum. Donec nec tincidunt dolor. Mauris dolor nisi, pellentesque quis tellus malesuada, placerat tristique dui. Phasellus at dui in nunc vestibulum accumsan. Aliquam erat volutpat. Duis non interdum ipsum, et tristique leo. Pellentesque condimentum, felis id placerat placerat, purus purus mattis urna, ac sodales ipsum ipsum ut risus. Aenean eget molestie ante, nec blandit magna. Integer efficitur fringilla odio sit amet sagittis. Sed ante turpis, pulvinar condimentum vehicula ac, tempus cursus ante. Donec eu scelerisque metus. Vestibulum facilisis tincidunt fermentum. Phasellus consequat odio in libero pharetra rutrum.</p>

        <hr />

        <h3>Education</h3>
        <ul class="list-unstyled list-spaced">
            <li>Ph.D., Field of Degree, University of Lorem Ipusm, 1990</li>
            <li>M.S., Field of Degree, University of Lorem Ipusm, 1990</li>
            <li>B.S., Field of Degree, University of Lorem Ipusm, 1990</li>
        </ul>

        <hr />

        <h3>Classes Taught</h3>
        <ul class="list-unstyled list-spaced">
            <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - First Class</a></li>
            <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Second Class</a></li>
            <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Third Class</a></li>
        </ul>

        <div id="classes" class="collapse">

            <ul class="list-unstyled list-spaced top-0">
                <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Fourth Class</a></li>
                <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Fifth Class</a></li>
                <li><a target="_blank" href="http://catalog.example.com/search/?search=CLA+###">CLA ### - Sixth Class</a></li>
            </ul>

        </div>

        <button type="button" class="btn btn-default expand collapsed" data-toggle="collapse" data-target="#classes"></button>

        <hr />

        <h3>Affiliations</h3>
        <ul class="list-unstyled list-spaced">
            <li>Organization</li>
            <li>Organization</li>
            <li>Organization</li>
        </ul>

        <div id="affiliations" class="collapse">

            <ul class="list-unstyled list-spaced top-0">
                <li>Organization</li>
                <li>Organization</li>
                <li>Organization</li>
            </ul>

        </div>

        <button type="button" class="btn btn-default expand collapsed" data-toggle="collapse" data-target="#affiliations"></button>

        <hr />

        <h3>Publications</h3>
        <ul class="list-unstyled list-spaced">
            <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
            <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
            <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
        </ul>

        <div id="publications" class="collapse">

            <ul class="list-unstyled list-spaced top-0">
                <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
                <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
                <li>"Article Title." <em>Name of Publication.</em> Volume, Page Numbers. Authors (Date).</li>
            </ul>

        </div>

        <button type="button" class="btn btn-default expand collapsed" data-toggle="collapse" data-target="#publications"></button>

    </div>

    <aside class="col-xs-12 col-sm-4 col-md-3">
        <div class="well">

            <h3>Contact</h3>
            <p>Address <br />Building Name <br />Room # <br />City, State, Zip <br /> <a href="/map"><i class="ico-map"></i>Map</a></p>
            <p><i class="ico-phone"></i> XXX.XXX.XXXX <br /> <a href="mailto:">name@example.com</a></p>
        </div>

        <h3>Academic Interests</h3>
        <ul class="list-unstyled list-spaced">
            <li>Area</li>
            <li>Another Area</li>
            <li>Area</li>
            <li>Another Area</li>
        </ul>

    </aside>

</div>"""

        # write to file
        f.write(message)

# close file
f.close()

# open in browser
filename = 'template.html'
webbrowser.open_new_tab(filename)