Python CSV到HTML表代码不起作用
我的代码目前看起来像这样。xls到csv部分的转换是有效的,但不能写入HTMLPython CSV到HTML表代码不起作用,python,html,csv,Python,Html,Csv,我的代码目前看起来像这样。xls到csv部分的转换是有效的,但不能写入HTML import xlrd import csv import sys # write from xls file to csv file wb = xlrd.open_workbook('your_workbook.xls') sh = wb.sheet_by_name('Sheet1') your_csv_file = open('your_csv_file.csv', 'wb') wr = csv.writer(
import xlrd
import csv
import sys
# write from xls file to csv file
wb = xlrd.open_workbook('your_workbook.xls')
sh = wb.sheet_by_name('Sheet1')
your_csv_file = open('your_csv_file.csv', 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in xrange(sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
print "Converted from xls to csv!"
# write from csv file to html
# if len(sys.argv) < 3:
# print "Usage: csvToTable.py csv_file html_file"
# exit(1)
# Open the CSV file for reading
reader = csv.reader(open("your_csv_file.csv"))
# Create the HTML file for output
htmlfile = open("data.html","w+")
# initialize rownum variable
rownum = 0
# generate table contents
for row in reader: # Read a single row from the CSV file
for line in htmlfile:
# this HTML comment is found in the HTML file where I want to insert the table
if line == "<!-- Table starts here !-->":
# write <table> tag
htmlfile.write('<table>')
htmlfile.write('<tr>') # write <tr> tag
for column in row:
htmlfile.write('<th>' + column + '</th>')
htmlfile.write('</tr>')
# write </table> tag
htmlfile.write('</table>')
#increment row count
rownum += 1
# print results to shell
print "Created " + str(rownum) + " row table."
exit(0)
导入xlrd
导入csv
导入系统
#从xls文件写入csv文件
wb=xlrd.open_工作簿(“your_workbook.xls”)
sh=wb.sheet\u by\u名称(“Sheet1”)
您的\u csv\u文件=打开('your\u csv\u file.csv','wb')
wr=csv.writer(您的“csv”文件,quoting=csv.QUOTE\u ALL)
对于xrange中的rownum(sh.nrows):
wr.writerow(sh.row_值(rownum))
您的\u csv\u文件。关闭()
打印“从xls转换为csv!”
#从csv文件写入html
#如果len(sys.argv)<3:
#打印“用法:csvToTable.py csv\u文件html\u文件”
#出口(1)
#打开CSV文件进行读取
reader=csv.reader(打开(“您的\u csv\u文件.csv”))
#创建用于输出的HTML文件
htmlfile=open(“data.html”,“w+”)
#初始化rownum变量
rownum=0
#生成表内容
对于读卡器中的行:#从CSV文件中读取一行
对于htmlfile中的行:
#此HTML注释位于我要插入表的HTML文件中
如果行==“”:
#写标签
htmlfile.write(“”)
htmlfile.write(“”)#write标记
对于行中的列:
htmlfile.write(“”+列+“”)
htmlfile.write(“”)
#写标签
htmlfile.write(“”)
#增量行计数
rownum+=1
#将结果打印到shell
打印“已创建”+str(rownum)+“行表”
出口(0)
输出只是一个空白页,因为程序找不到
<!-- Table starts here !-->
尝试将读取模式从“w+”更改为“a+”:
htmlfile=open(“data.html”、“a+”)
当您以w+
模式打开文件data.html
时,该文件会被截断,然后当您读取htmlfile:中的行的行时,您将找不到”
html注释
还要添加line.strip()
以读取字符串末尾不带换行符的行:
if line.strip() == "<!-- Table starts here !-->":
正如Divertriy所说,您的读取模式不正确:
w+:打开文件进行写入和读取。覆盖
现有文件(如果文件存在)。如果文件不存在,则创建
用于读写的新文件
所以它要做的第一件事就是截断(清空)整个文件 从htmlfile
读取的行包括一个尾随的换行符。在比较之前,您必须先选择它:
if line.strip() == "<!-- Table starts here !-->":
但是第二个代码>非常少见。这里有两三个问题。我将逐一介绍它们,但首先我想说,我将使用。它所做的远远超过了这类任务,但如果您确实安装了它,那么要将数据转换为表格式,您所要做的就是:
import pandas as pd
xls = pd.ExcelFile('path_to_file.xls')
df = xls.parse('Sheet1') # parse the sheet you're interested in - results in a Dataframe
table_html = df.to_html()
现在,您有了一个html格式的整个数据的字符串(table_html
),可以直接写入html文件。没有中间阶段或任何东西。该文档可用于和
现有解决方案的问题
1.字符串比较
您正在寻找要替换为html的注释行-您正在使用==
比较两个字符串。除非您完全确定字符串是完全相同的-没有额外的空格,没有行尾,没有额外的标点符号等等-否则这通常是容易出错的
您可以strip()
删除行中的空格,然后按照其他人的建议使用=
。就我个人而言,我会倾向于更加宽容,在
关键字中使用,比如:
if '<!-- Table starts here' in line:
注意我假设您在一开始就使用Pandas方法得到了表格html
。如果您出于某种原因不想这样做,并且仍然希望通过csv
获取内容,您可以通过创建一个空字符串,然后以类似于现在循环的方式添加所有html元素,来构建表html
3.编写html
其他人注意到,您可以使用文件打开的附加模式,而不是写入模式。这很好,但如果使用上述方法将所有内容读取到列表中并插入列表中,则只需执行以下操作:
with open('data.html', 'w+') as f:
f.write('\n'.join(content))
你似乎从零开始写了很多东西,所有这些功能都已经存在于一个很棒的名为pandas的库中:我这么做了,它根本没有改变HTML文件。我想这比使用空白页要好。请检查HTML文件中是否存在“”。确实如此。我认为for循环有一些问题,我正在重新检查。我按照Tichodroma的建议添加了它,但仍然没有骰子。非常感谢!这样的答案让这个网站如此棒。但是有一件事,假设行和列的格式相同,我如何使新的电子表格的值可以写在HTML中的旧表上?噢,这是一个不同的(而且更难)问题。我的方法是在末尾添加一个额外的注释标记(即插入table_html
),然后调整代码,以便在检测到表开始标记时,在检测到表结束标记之前不会添加到内容中。其他一切都将保持不变。试一试——如果你遇到更多问题,是时候问一个新的StackOverflow问题了:)当然,你可以在这里链接到它,但这个问题太大了,无法在评论/编辑中对现有问题进行充分解释。
if '<!-- Table starts here' in line:
content = []
insert_index = None
with open('data.html', 'r') as htmlfile:
for line in htmlfile:
content.append(line)
if '<!-- Table starts here' in line:
insert_index = len(content)
if insert_index:
content.insert(insert_index, table_html)
with open('data.html', 'w+') as f:
f.write('\n'.join(content))