Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python csv

import csv

with open('data.csv', 'r') as f:
    reader = csv.reader(f)
    # loop through each row and print each value
    for row in reader:
        for e in row:
            print(e)

with open('data.csv', 'r') as f:
    # change the delimiter from the default comma to another delimiter
    reader = csv.reader(f, delimiter="|")

    for row in reader:

        for e in row:
            print(e)

nums = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]

with open('numbers2.csv', 'w') as f:
    writer = csv.writer(f)
    # write arrays as rows to CSV file
    for row in nums:
        writer.writerow(row)


with open('numbers2.csv', 'w') as f:

    writer = csv.writer(f, delimiter="+")
    # write arrays as row to CSV file with + as the delimiter instead of commas
    for row in nums:
        writer.writerow(row)
Comment

python csv

import csv

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

with open('countries.csv', 'w', encoding='UTF8', newline='') as f:
    writer = csv.writer(f)
    # write the header
    writer.writerow(header)
    # write the data
    writer.writerow(data)
Comment

csv python

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

pythone csv

>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
Comment

csv python

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

csv python

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

python csv

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

csv in python

import csv
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
with open('passwd', newline='') as f:
    reader = csv.reader(f, 'unixpwd')
Comment

csv python

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

csv python

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

csv python

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

python csv

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

csv python

import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
Comment

csv file python

this is for csv file python
Comment

csv python

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

csv python

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Comment

PREVIOUS NEXT
Code Example
Python :: extract email address using expression in django 
Python :: pandas legend placement 
Python :: python fstring 
Python :: subtract number from each element in list python 
Python :: pydrive upload file to folder 
Python :: python functions with input 
Python :: how to make exe from.py file 
Python :: add two numbers in python 
Python :: unique list values python ordered 
Python :: open csv from url python 
Python :: how to convert each string to a category or int in python dataframe 
Python :: how to check substring in python 
Python :: django or 
Python :: print inline output in python 
Python :: how to use setattr Python 
Python :: matplotlib vertical tick labels 
Python :: convert all colnames of dataframe to upper 
Python :: notna pandas 
Python :: convert timedelta to days 
Python :: pandas merge two columns from different dataframes 
Python :: get last n in list python 
Python :: remove in list python 
Python :: import ndimage 
Python :: convert plt image to numpy 
Python :: get scipy version python 
Python :: remove space from string python 
Python :: import picturein colab 
Python :: write cell output to file jupyter colab 
Python :: generate random integers 
Python :: df to sql mysql 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =