Bottle.py - 拆分字符串数组(CSV)

时间:2017-05-22 09:07:10

标签: bottle

我有一个小型网络演示,将数据从CSV文件中提取出来,我想在网页上显示。我打算使用Bottle.py,但我无法将字符串拆分为数组。 CSV文件中将有多行,我将在拆分时读取,然后创建一个表。

from bottle import run, debug, template, get, redirect, request, response, static_file, route, os, sys
LOG_FILENAME = "data.csv"
@route('/hello/<name>')
def index(name):
    readfiledata()
    print(reader)
    array = reader.split(',')

return template('<b>Hello {{name}}</b>!', name=reader)

1 个答案:

答案 0 :(得分:1)

也许是这样的(使用标准库中的import csv from bottle import route, jinja2_template @route('/csv') def show_csv(): with open('data.csv') as fo: csv_reader = csv.reader(fo) table = [row for row in csv_reader] return jinja2_template('templ.html', table=table) 模块):

templ.html

<table style="width:100%"> <tr> {# Table header #} <th>Foo</th> <th>Bar</th> <th>Baz</th> </tr> {% for row in table %} <tr> {% for cell in row %} <td>{{ cell }}</td> {% endfor %} </tr> {% endfor %} </table> (我使用的是Jinja2模板,我更喜欢使用Bottle的内置模板语言):

    private static Dictionary<string, object> keys = new Dictionary<string, object>();
    private static object lockKeyObject = true;
    private static object ContextResolver(Ninject.Activation.IContext context)
    {
        var id = string.Format("{0}_{1}",
            Thread.CurrentThread.ManagedThreadId, 
            System.Threading.Tasks.Task.CurrentId ?? -1);

        lock (lockKeyObject)
        {
            if (keys.ContainsKey(id) == false)
            {
                keys.Add(id, (object)id);
            }
        }

        return keys[id];
    }