Código de Python - CSV to HTML

Requerimientos

python >= 2.7

1.0.0
estrellaestrellaestrellaestrellaestrella(2)

Publicado el 10 de Diciembre del 2017gráfica de visualizaciones de la versión: 1.0.0
1.927 visualizaciones desde el 10 de Diciembre del 2017
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import csv
from string import Template
 
html_template = Template("""
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>$title</title>
    <style>
        * {margin: 0; padding: 0; font-size: 16px; box-sizing: border-box;}
        body {font-family: Helvetica, sans-serif; color: #212121}
        table {border-collapse: collapse; width: 100%;}
        table, td {border: 1px solid #acacac;}
        td { padding: .3rem;}
        .datasheet {max-width: 960px; width: 100%; margin: 1rem auto;
        overflow: auto;}
    </style>
</head>
<body>
    <div class="datasheet">
        <table>$content</table>
    </div>
</body>
</html>""")


def csv_to_html(csv_file, dest_filename='index.html', title='Document'):
    """ Convierte un archivo csv a una tabla en formato html. """

    with open(csv_file, mode='r') as f:
            # detecto el dialecto utilizado por el archivo
            dialect = csv.Sniffer().sniff(f.read(1024))
            f.seek(0)
            csv_reader = csv.reader(f, dialect)

            rows = ''
            for line in csv_reader:
                cols = ''
                for field in line:
                    cols += '<td>{}</td>'.format(field)
                rows += '<tr>{}</tr>'.format(cols)

    # creo el archivo html
    with open(dest_filename, mode='w') as f:
            content = html_template.substitute(title=title, content=rows)
            f.write(content)



Comentarios sobre la versión: 1.0.0 (2)

Imágen de perfil
10 de Diciembre del 2017
estrellaestrellaestrellaestrellaestrella
No ha dejado ningún comentario
Responder
Imágen de perfil
10 de Diciembre del 2017
estrellaestrellaestrellaestrellaestrella
No ha dejado ningún comentario
Responder

Comentar la versión: 1.0.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s4338