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

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 :: moving average pandas 
Python :: repeat array along new axis 
Python :: how to change plot size in matplotlib 
Python :: python data structures 9.4 
Python :: how to run linux command in python 
Python :: change to first letter capital list python 
Python :: python string in set 
Python :: pvm python 
Python :: feature importance plot 
Python :: drupal 8 request_time 
Python :: iterate through characters in a string python 
Python :: numpy stack arrays vertically 
Python :: df rename columns 
Python :: find character in python 
Python :: select a range of rows in pandas dataframe 
Python :: get absolute url django 
Python :: standard deviation python 
Python :: Python pandas first and last element of column 
Python :: 2d dictionary in python 
Python :: how to read a csv file in python 
Python :: get title beautifulsoup 
Python :: dictionary size in python 
Python :: ComplexWarning: Casting complex values to real discards the imaginary part 
Python :: discord py get all channels in guild 
Python :: pandas index between time 
Python :: how to use global variable in python 
Python :: how to plot confusion matrix 
Python :: push to pypi 
Python :: how to remove spaces in string in python 
Python :: convert 1 to "one" python 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =