我想从html
中提取表格数据并保存为text file
import urllib2, numpy as np, pandas as pd
fo = 'fo.txt'
url = 'https://coinmarketcap.com/currencies/bitcoin/historical-data/'
html = urllib2.urlopen(url).read()
rows = pd.read_html(html)
print type(rows)
print rows
for row in rows:
this_row = "|".join([str(td) for td in row])
fo.write(this_row + "\n")
但得到了错误:
Traceback (most recent call last):
fo.write(this_row + "\n")
AttributeError: 'str' object has no attribute 'write'
文本文件中生成的表格数据看起来与原始链接相同: https://coinmarketcap.com/currencies/bitcoin/historical-data/
请帮忙!
答案 0 :(得分:1)
如果要写入文本文件,则需要文件对象。在您的源代码中,fo
对象是string
。
在python中,你可以打开一个文件来写这样的文字:
with open(fo,'w') as text_file:
for row in rows:
this_row = row
text_file.write(this_row + "\n")