Answers for "export list to csv python"

2

how to convert list into csv in python

import pandas as pd    

list1 = [1,2,3,4,5]
df = pd.DataFrame(list1)
df.to_csv('filename.csv', index=False)

#index =false removes unnecessary indexing/numbering in the csv
Posted by: Guest on June-15-2020
2

how to create a list from csv python

import csv

with open('file.csv', newline='') as f:
    reader = csv.reader(f)
    data = list(reader)

print(data)
Posted by: Guest on November-08-2020
2

list to csv

import csv

with open("out.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(a)
Posted by: Guest on September-22-2020
0

export html table to csv python

# Importing the required modules  
import os 
import sys 
import pandas as pd 
from bs4 import BeautifulSoup 
   
path = 'html.html'
   
# empty list 
data = [] 
   
# for getting the header from 
# the HTML file 
list_header = [] 
soup = BeautifulSoup(open(path),'html.parser') 
header = soup.find_all("table")[0].find("tr") 
  
for items in header: 
    try: 
        list_header.append(items.get_text()) 
    except: 
        continue
  
# for getting the data  
HTML_data = soup.find_all("table")[0].find_all("tr")[1:] 
  
for element in HTML_data: 
    sub_data = [] 
    for sub_element in element: 
        try: 
            sub_data.append(sub_element.get_text()) 
        except: 
            continue
    data.append(sub_data) 
  
# Storing the data into Pandas 
# DataFrame  
dataFrame = pd.DataFrame(data = data, columns = list_header) 
   
# Converting Pandas DataFrame 
# into CSV file 
dataFrame.to_csv('MyTable.csv')
Posted by: Guest on December-07-2020

Python Answers by Framework

Browse Popular Code Answers by Language