Convert HTML into CSV

后端 未结 5 1876

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:27

    Use the csv module from Python to do this. You can obviously write more columns if you want, but the idea is that you're writing a list to the csv file. There are other options that you can specify in the writer() method if you'd like to quote things, escape things, etc.

    import csv
    
    with open('your_csv_name.csv', 'w') as o:
        w = csv.writer(o)
        # Headers
        w.writerow(['tr_content'])
        # Write the tr text
        for r in rows:
            w.writerow([r])
    

提交回复
热议问题