Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

csv python write

import csv

with open('names.csv', 'w') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
    writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
    writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
Comment

python writing to csv file

import csv  

header = ['name', 'area', 'country_code2', 'country_code3']
data = ['Afghanistan', 652090, 'AF', 'AFG']

with open('countries.csv', 'w', encoding='UTF8') as f:
    writer = csv.writer(f)

    # write the header
    writer.writerow(header)

    # write the data
    writer.writerow(data)
Comment

python write to file csv

import csv  

header = ['name', 'area', 'country_code2', 'country_code3']
data = ['Afghanistan', 652090, 'AF', 'AFG']

with open('countries.csv', 'w', encoding='UTF8') as f:
    writer = csv.writer(f)

    # write the header
    writer.writerow(header)

    # write the data
    writer.writerow(data)
Comment

csv writer python

import csv

# csv header
fieldnames = ['name', 'area', 'country_code2', 'country_code3']

# csv data
rows = [
    {'name': 'Albania',
    'area': 28748,
    'country_code2': 'AL',
    'country_code3': 'ALB'},
    {'name': 'Algeria',
    'area': 2381741,
    'country_code2': 'DZ',
    'country_code3': 'DZA'},
    {'name': 'American Samoa',
    'area': 199,
    'country_code2': 'AS',
    'country_code3': 'ASM'}
]

with open('countries.csv', 'w', encoding='UTF8', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(rows)
Code language: PHP (php)
Comment

PREVIOUS NEXT
Code Example
Python :: python input tuple from user 
Python :: how to use Qtimer in thread python 
Python :: count missing values groupby 
Python :: python get home path 
Python :: dataframe groupby to dictionary 
Python :: how to save data to text file python 
Python :: python nameerror input 
Python :: browser pop up yes no selenium python 
Python :: how to check if user is using main file or importing the file and using in python 
Python :: ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject 
Python :: ros python subscriber 
Python :: entropy python 
Python :: python round number numpy 
Python :: how to get the index of a value in pandas dataframe 
Python :: How to ungrid something tkinter 
Python :: new working version of linkchecker 
Python :: pythonic 
Python :: pandas sort values by multiple columns 
Python :: how to wait in pygame 
Python :: np array describe 
Python :: how do you create a countdown using turtle python 
Python :: add empty column to dataframe pandas 
Python :: how to visualize decision tree in python 
Python :: cv2.adaptiveThreshold() python 
Python :: goal parser 
Python :: python system of nonlinear equations 
Python :: pandas groupby aggregate quantile 
Python :: calcolatrice online 
Python :: date parser python pandas 
Python :: python discord how to get user variables 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =