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 :: build url python 
Python :: how to get height in pyqt5 
Python :: frequency unique pandas 
Python :: python read live radio 
Python :: change column value based on another column pandas 
Python :: python tkinter go to another window on button click 
Python :: python faker 
Python :: error bar plot python 
Python :: rename columns in dataframe 
Python :: import a txt file into python 
Python :: run 2 loops simultaneously python 
Python :: print random word python 
Python :: python check version 
Python :: sns legend outside 
Python :: converting pandas._libs.tslibs.timedeltas.Timedelta to days 
Python :: how to create data dictionary in python using keys and values 
Python :: make longitude -180 to 180 
Python :: first day of the month python 
Python :: how to log ip addresses in django 
Python :: plt show 2 images 
Python :: password combination python 
Python :: first row as column df 
Python :: raise an APi error on django rest view 
Python :: convert image to matrix python 
Python :: python ignore exception 
Python :: The following code shows how to reset the index of the DataFrame and drop the old index completely: 
Python :: python csv read header only 
Python :: spyder 3.3.6 requires pyqtwebengine<5.13; python_version = "3", which is not installed. 
Python :: python write txt utf8 
Python :: median absolute deviation python 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =