我是python新手,我想知道如何编写一个包含简单列表的CSV文件。然后我将在Excel工作表中使用此文件。在
我的全部代码:
import csv
import urllib
from bs4 import BeautifulSoup
sock = urllib.urlopen("http://www.fatm.com.es/Datos_Equipo.asp?Cod=03CA0007")
htmlSource = sock.read()
sock.close()
soup = BeautifulSoup(htmlSource)
form = soup.find("form", {'id': "FORM1"})
table = form.find("table")
entidad = [item.text.strip() for item in table.find_all('td')]
valores = [item.get('value') for item in form.find_all('input')]
lista = entidad
i = 0
x = 1
while i <=10:
lista.insert(i+x, valores[i])
i += 1
x += 1
print lista
w = csv.writer(file(r'C:\Python27\yo.csv','wb'),dialect='excel')
w.writerows(lista)
我的结果:一个空的CSV文件。:(
提前谢谢!!!在
给你:
作为后续问题。代替您,我将为您的列名创建一个列表,例如:
^{pr2}$以及以下值的列表:
然后您可以:
^{4}$检查csv模块的doc,可能有一种方法可以一次性写入所有行。我自己从来没用过。在
相关问题 更多 >
编程相关推荐