Answers for "python cread csv dict herader only"

16

create csv file python

# This action requires the 'csv' module
import csv

# The basic usage is to first define the rows of the csv file:
row_list = [["SN", "Name", "Contribution"],
             [1, "Linus Torvalds", "Linux Kernel"],
             [2, "Tim Berners-Lee", "World Wide Web"],
             [3, "Guido van Rossum", "Python Programming"]]

# And then use the following to create the csv file:
with open('protagonist.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(row_list)
# This will create a csv file in the current directory
Posted by: Guest on February-24-2020
0

python read scv

import csv
with open('eggs.csv', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print(', '.join(row))

# Output:
# Spam, Spam, Spam, Spam, Spam, Baked Beans
# Spam, Lovely Spam, Wonderful Spam
Posted by: Guest on December-01-2020
0

accessing elements in DictReader

import csv

input_file = csv.DictReader(open("people.csv"))

max_age = None
oldest_person = None
for row in input_file:
    age = int(row["age"])
    if max_age == None or max_age < age:
        max_age = age
        oldest_person = row["name"]

if max_age != None:
    print "The oldest person is %s, who is %d years old." % (oldest_person, max_age)
else:
    print "The file does not contain any people."
Posted by: Guest on October-21-2020

Python Answers by Framework

Browse Popular Code Answers by Language