用于导出csv文件的python脚本 - 未在tablepress

时间:2015-08-07 11:56:44

标签: python excel csv

我试图解决这个问题,但它只是不起作用。代码导出一个.csv文件,可以在excel中查看得很好(但它每行都跳过一行......),但是当我上传到tablepress或websimontables时,代码将所有行放在一个单元格而不是每个单元格中单元格导致一个看起来很近的表格。

你可以看到我在这里谈论的一个例子http://www.dustindavisyourrealtor.com/2015/08/06/85/。顶部表格是从这个脚本输出的,底部表格是我正在寻找的一个例子。

以下是输入.csv文件的链接 https://www.dropbox.com/s/figher1vgnlmwju/realtor.csv?dl=0

运行脚本:python conv.py input.csv output.csv

import sys
import re
import csv
import urllib

CHARS_TO_REMOVE = (
    0xa0, 0xae
)
RE1 = re.compile('^\d+$')
RE2 = re.compile('^\w{3}\s\d+\/\d+$')
FLAG1 = 'f1'
FLAG2 = 'f2'
URL_BASE = 'https://www.instantstreetview.com/s/%s'
EMAIL_LINK = '<a href=\'https://mail.google.com/mail/?view=cm&fs=1&tf=1&to=email@here.ca%20&su=Hello%20again\'>I want more information about this house</a>'


def cleanLine(line):
    for ch in CHARS_TO_REMOVE:
        line = line.replace(chr(ch), '')
    return line.strip()

def checkRow(row):
    m1 = RE1.match(row[0])
    if m1:
        return FLAG1
    else:
        m2 = RE2.match(row[0])
        if m2:
            return FLAG2
    return None

def formatRow(row):
    Addr= ' '.join(row['row1'][5:8])
    Url = URL_BASE % urllib.quote_plus(Addr)
    Row = {
        #'Date' : row['row2'][0],
        #'Time' : row['row2'][1],
        'Date / Time' : ' '.join(row['row2'][0:2]),
        'Type' : row['row1'][1],
        'Beds' : row['row1'][2],
        'Bath' : row['row1'][3],
        'Address' : row['row1'][5] + ', ' + row['row1'][6],
        'Listed At' : row['row1'][4],
        'Area' : '<a href=\'%s\'>See the neighbourhood</a>' % Url,
        'Get In Touch!' : EMAIL_LINK
    }

    return Row

def main(fnameIn, fnameOut):

    Rows = []
    row1 = None
    row2 = None
    for row in csv.reader(file(fnameIn).readlines()):
        row = map(cleanLine, row)
        flag = checkRow(row)
        if flag == FLAG1:
            row1 = row
            row2 = None
        elif flag == FLAG2:
            row2 = row

        if row1 and row2:
            Rows.append(dict(row1=row1, row2=row2))
            row1 = row2 = None

    with open(fnameOut, 'w') as f:

        fieldnames = ['Date / Time', 'Type', 'Beds', 'Bath', 'Address', 'Listed At', 'Area', 'Get In Touch!']

        writer = csv.DictWriter(f, fieldnames=fieldnames, quoting=csv.QUOTE_ALL)

        writer.writeheader()

        for row in Rows:
            writer.writerow(formatRow(row))



if __name__ == '__main__':
    main(sys.arg[1], sys.argv[2])

0 个答案:

没有答案