Convert HTML into CSV

后端 未结 5 1873

I want to convert a HTML table as obtained from the script below into a CSV file, but got type error as follows:

TypeError: sequence item 0: expected

5条回答
  •  情歌与酒
    2020-11-29 08:14

    Here is another way without using csv module:

    fp=open('data.csv','w')
    for row in rows[:-1]:   # Removed last row as it has empty cells that gives error which can also be resolved if needed 
          fp.write(row.get_text(',') + '\n')
    fp.close()
    

    You can directly open data.csv file.

    Station details can be get by below command:

    >>>> table = soup.find_all('table', class_='data2_s')
    >>>> print table[0].find_all('caption')[0].get_text().encode('ascii', 'ignore')
    WAKKANAI   WMO Station ID:47401 Lat 45o24.9'N  Lon 141o40.7'E
    

    Hope this helps.

提交回复
热议问题