data.csv
- no title specified Programming language | Designed by | Appeared | Extension |
Python | Guido van Rossum | 1991 | .py |
Java | James Gosling | 1995 | .java |
C++ | Bjarne Stroustrup | 1983 | .cpp |
['\ufeffProgramming language', 'Designed by', 'Appeared', 'Extension'] ['Python', 'Guido van Rossum', '1991', '.py'] ['Java', 'James Gosling', '1995', '.java'] ['C++', 'Bjarne Stroustrup', '1983', '.cpp']
# the separator character is called a delimiter, and the comma is not
# the only one used. Other popular delimiters include the tab (\t),
# colon (:) and semi-colon (;) characters. Properly parsing a CSV file
# requires us to know which delimiter is being used.
with open('./data.csv', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in data:
print(','.join(row))
Programming language,Designed by,Appeared,Extension Python,Guido van Rossum,1991,.py Java,James Gosling,1995,.java C++,Bjarne Stroustrup,1983,.cpp
employee_addresses.txt
name,address,date joined
john smith,1132 Anywhere Lane Hoboken NJ*, 07030,Jan 4
erica meyers,1234 Smith Lane Hoboken NJ*, 07030,March 2
# The reader object can handle different styles of CSV files by
# specifying additional parameters, some of which are shown below:
# delimiter specifies the character used to separate each field.
# The default is the comma (',').
# quotechar specifies the character used to surround fields that
# contain the delimiter character. The default is a double quote
# (' " ').
# escapechar specifies the character used to escape the delimiter
# character, in case quotes aren’t used. The default is no escape
# character.
with open('./employee_addresses.txt', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=',', quotechar='|', escapechar='/')
for row in data:
print(','.join(row))
name,address,date joined john smith,1132 Anywhere Lane Hoboken NJ*, 07030,Jan 4 erica meyers,1234 Smith Lane Hoboken NJ*, 07030,March 2
bday.csv
- no title specified John Smith | Accounting | November |
Erica Meyers | IT | March |
| name | department | birthday month |
|---|---|---|
| John Smith | Accounting | November |
| Erica Meyers | IT | March |
with open('bday.csv', mode='w') as employee_file:
employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
employee_writer.writerow(['John Smith', 'Accounting', 'November'])
employee_writer.writerow(['Erica Meyers', 'IT', 'March'])
employee_file2.csv
- no title specified emp_name | dept | birth_month |
John Smith | Accounting | November |
Erica Meyers | IT | March |
employee_addresses.csv
- no title specified name | address | date joined |
john smith | 1132 Anywhere Lane Hoboken NJ, 07030 | Jan 4 |
erica meyers | 1234 Smith Lane Hoboken NJ, 07030 | March 2 |
import csv
with open('employee_addresses.txt', newline='') as infile, open('employee_addresses.csv', mode='w', newline='') as outfile:
reader = csv.DictReader(infile, delimiter=',', quotechar='|', escapechar='*')
writer = csv.DictWriter(outfile, reader.fieldnames, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
# copy rows over to new format
writer.writeheader()
writer.writerows(reader)
# with a loop which is not necessary;
import os
import csv
with open('./employee_addresses.txt', newline='') as csvfile:
data = csv.reader(csvfile, delimiter=',', quotechar='|', escapechar='*')
table = []
for row in data:
table.append(row)
with open('employee_addresses2.txt', mode='w') as employee_file:
employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
employee_writer.writerows(table)
os.rename(r'employee_addresses2.txt', r'employee_addresses.csv')
import os
os.rename(r'employee_addresses2.txt', r'employee_addresses.csv')
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) <ipython-input-11-23ba5a5622be> in <module> 1 import os ----> 2 os.rename(r'employee_addresses2.txt', r'employee_addresses.csv') FileNotFoundError: [Errno 2] No such file or directory: 'employee_addresses2.txt' -> 'employee_addresses.csv'
employee_birthday.txt
name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March
# or else:
with open('employee_birthday.txt', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.')
line_count += 1
print(f'Processed {line_count} lines.')
# Where did the dictionary keys come from? The first line of the CSV
# file is assumed to contain the keys to use to build the dictionary.
# If you don’t have these in your CSV file, you should specify your
# own keys by setting the fieldnames optional parameter to a list
# containing them.
import os
print (os.getcwd()) # Prints the current working directory
os.chdir('.') # Provide the new path here
print (os.getcwd())
pwd