Files in python csv files with python

How to Read CSV in Python, Write and Append Too

Ransomware recovery test drive: This technical workshop is designed to take you behind the scenes and shows you how to adopt strategies to automate recovery, ensuring you’re ready to become a recovery hero. REQUEST YOUR LAB

If you need to read CSV in Python like reading CSV files (and writing) to them, you’re in luck. In this tutorial, you’re going to learn how to read, write to and append data to CSV files in your Python scripts!

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

  • A Windows or Linux host with Python 3 installed. This tutorial will use Windows, but Linux will work fine also.
  • A code editor like VS Code to copy and paste Python code snippets to.

How to Read CSV in Python

Let’s get started and see how you can read CSV in Python. Python provides a built-in module called csv that has various methods allowing you to work with CSV files so let’s use that.

Читайте также:  Тернарный оператор си шарп

To read CSV files, the Python csv module provides a method called reader() . Let’s first demonstrate how to use this method.

1. Create a directory at ~/pythoncsvdemo and download this csv file into it. The example CSV contains a list of fictitious people with columns of “Name,” “Sex,” “Age,” “Height (in),” and “Weight (lbs).” This CSV file will be used throughout this tutorial.

2. Next, open a code editor, paste the following Python script into it. This simple script imports the csv module and uses the reader() method to read the file and iterate over each line in the CSV file with a for loop.

import csv #import to use the csv module with open('demo_csv.csv', mode="r") as csv_file: #"r" represents the read mode reader = csv.reader(csv_file) #this is the reader object for item in reader: # you have to loop through the document to get each data print(item)

Replace the reader() method with the dictreader() method to return CSV rows in a Python dictionary rather than an array.

In the below output, you’ll see the first line is the name of the columns, with each row representing a CSV row. Each column represents an index starting from 0.

CSV output via the reader() method

CSV output via the dicteader() method

3. Perhaps you’d prefer only to see the output of one column. No problem. Provide the index number of 1 against the item variable representing the row.

import csv with open('demo_csv.csv', mode="r") as csv_file: reader = csv.reader(csv_file) for item in reader: print(item[1])# index is added to get a particular column

If you’re using the dictreader() method, replace the print() command above with print(item[«Name»]) . Since dictreader() creates a dictionary for each CSV row, you can reference columns in the row by name instead of index number.

Display list of Sexes

Creating CSV Files with Python

Using the csv module and a few handy methods, you can also write to CSV files from Python. To write a CSV file, create another script with the following code.

This script defines each column that will be in the CSV ( column_name ) along with each element in a single row ( data ). The script then opens the demo_csv1.csv for writing ( w ) and writes a single row ( writerow() ).

import csv column_name = ["Name", "Sex", "Age", "Height (in)", "Weight (lbs)"] #The name of the columns data = ['Ali',"M", 29, 71,176] #the data with open('demo_csv1.csv', 'w') as f: writer = csv.writer(f) #this is the writer object writer.writerow(column_name) # this will list out the names of the columns which are always the first entrries writer.writerow(data) #this is the data

If you need to append row(s) to a CSV file, replace the write mode ( w ) with append mode ( a ) and skip writing the column names as a row ( writer.writerow(column_name) ).

You’ll see below that Python creates a new CSV file (demo_csv1.csv), with the first row contains the column names and the second row includes the first row of data.

New CSV file created with Python

Appending to a CSV file with a Dictionary

If you’d prefer to use a dictionary, change your script slightly to use the dictwriter() method providing each field or column name as an argument ( fieldnames=field_names ), as shown below.

import csv # list of column names field_names = ['Name','Sex','Age','Height (in)','Weight (lbs)'] # Dictionary dict = with open('demo_csv.csv', 'a') as csv_file: dict_object = csv.DictWriter(csv_file, fieldnames=field_names) dict_object.writerow(dict)

Appending to a CSV file with a Dictionary

Reading from One CSV File to Write To Another

Perhaps you already have an existing CSV file and would like to use it as input to another CSV file. You can make it happen by using a combination of read mode and the reader() method and write mode and the writer() method.

  • Opens an existing file called demo_csv.csv in read mode
  • Reads the file as a CSV with the reader() method
  • Opens another CSV called new_demo_csv.csv in write mode
  • Reads each row in the source CSV file and writes those roles in the destination CSV file using the — delimiter.
import csv with open("demo_csv.csv", mode="r") as old_file: reader_obj = csv.reader(old_file) #read the current csv file with open("new_demo_csv.csv", mode="w") as new_file: writer_obj = csv.writer(new_file, delimiter="-") # Writes to the new CSV file for data in reader_obj: #loop through the read data and write each row in new_demo_csv.csv writer_obj.writerow(data)

Deleting Columns from CSV Files with Python

Let’s wrap up this tutorial by removing columns from CSV files. Unfortunately, eliminating columns isn’t as straightforward as reading or writing to CSV files, but you’ll see it still definitely possible.

To remove fields from a CSV file, you can’t directly remove them. Instead, you must read all of the fields in the CSV file and then write to another CSV file, excluding all fields you don’t want, like below.

import csv with open("demo_csv.csv", mode="r") as original: reader_obj = csv.reader(original) with open("output.csv", mode="w") as new: writer_obj = csv.writer(new) for column in reader_obj: writer_obj.writerow((column[0], column[1], column[2])) # this represents the columns you need

Conclusion

You should now have some foundational knowledge to read CSV in Python, write to CSV files, and even remove fields from CSV files. Using the Python csv module with its various methods, you can make quick work of CSV files in Python!

How do you plan to incorporate this newly discovered knowledge into your Python projects?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Источник

Как читать и писать CSV-файлы в Python

Esther Vaati

Esther Vaati Last updated Dec 5, 2017

Формат CSV является наиболее часто используемым форматом импорта и экспорта для баз данных и электронных таблиц. В этом руководстве будет подробно рассказано о CSV, а также о модулях и классах, доступных для чтения и записи данных в файлы CSV. Также будет рассмотрен рабочий пример, показывающий, как читать и записывать данные в файл CSV на Python.

Что такое файл CSV?

Файл CSV (значения, разделенные запятыми) позволяет сохранять данные в табличной структуре с расширением .csv. CSV-файлы широко используются в приложениях электронной коммерции, поскольку их очень легко обрабатывать. Некоторые из областей, где они были использованы, включают в себя:

  • импорт и экспорт данных клиентов
  • импорт и экспорт продукции
  • экспорт заказов
  • экспорт аналитических отчетов по электронной коммерции

Модули для чтения и записи

Модуль CSV имеет несколько функций и классов, доступных для чтения и записи CSV, и они включают в себя:

  • функция csv.reader
  • функция csv.writer
  • класс csv.Dictwriter
  • класс csv.DictReader

csv.reader

Модуль csv.reader принимает следующие параметры:

  • csvfile : обычно это объект, который поддерживает протокол итератора и обычно возвращает строку каждый раз, когда вызывается его метод __next__() .
  • dialect=’excel’: необязательный параметр, используемый для определения набора параметров, специфичных для определенного диалекта CSV.
  • fmtparams : необязательный параметр, который можно использовать для переопределения существующих параметров форматирования.

Вот пример того, как использовать модуль csv.reader.

with open('example.csv', newline='') as File: 

модуль csv.writer

Этот модуль похож на модуль csv.reader и используется для записи данных в CSV. Требуется три параметра:

  • csvfile : это может быть любой объект с методом write() .
  • dialect = ‘excel’ : необязательный параметр, используемый для определения набора параметров, специфичных для конкретного CSV.
  • fmtparam : необязательный параметр, который можно использовать для переопределения существующих параметров форматирования.

Классы DictReader и DictWriter

DictReader и DictWriter — это классы, доступные в Python для чтения и записи в CSV. Хотя они и похожи на функции чтения и записи, эти классы используют объекты словаря для чтения и записи в CSV-файлы.

DictReader

Он создает объект, который отображает прочитанную информацию в словарь, ключи которого задаются параметром fieldnames . Этот параметр является необязательным, но если он не указан в файле, данные первой строки становятся ключами словаря.

with open('name.csv') as csvfile: 
reader = csv.DictReader(csvfile) 
print(row['first_name'], row['last_name']) 

DictWriter

Этот класс аналогичен классу DictWriter и выполняет противоположную функцию: запись данных в файл CSV. Класс определяется как csv.DictWriter(csvfile, fieldnames,restval=», extrasaction=’raise’,dialect=’excel’, *args, **kwds)

Параметр fieldnames определяет последовательность ключей, которые определяют порядок, в котором значения в словаре записываются в файл CSV. В отличие от DictReader, этот ключ не является обязательным и должен быть определен во избежание ошибок при записи в CSV.

Диалекты и форматирование

Диалект — это вспомогательный класс, используемый для определения параметров для конкретного экземпляра reader или writer . Диалекты и параметры форматирования должны быть объявлены при выполнении функции чтения или записи.

Есть несколько атрибутов, которые поддерживаются диалектом:

  • delimiter: строка, используемая для разделения полей. По умолчанию это ‘,’ .
  • double quote: Управляет тем, как должны появляться в кавычках случаи, когда кавычки появляются внутри поля. Может быть True или False.
  • escapechar: строка, используемая автором для экранирования разделителя, если в кавычках задано значение QUOTE_NONE .
  • lineterminator: строка, используемая для завершения строк, созданных writer . По умолчанию используется значение ‘\r\n’ .
  • quotechar: строка, используемая для цитирования полей, содержащих специальные символы. По умолчанию это ‘»‘ .
  • skipinitialspace: Если установлено значение True , любые пробелы, следующие сразу за разделителем, игнорируются.
  • strict: если установлено значение True , возникает Error при неправильном вводе CSV.
  • quoting: определяет, когда следует создавать кавычки при чтении или записи в CSV.

Чтение файла CSV

Давайте посмотрим, как читать CSV-файл, используя вспомогательные модули, которые мы обсуждали выше.

Создайте свой CSV-файл и сохраните его как example.csv. Убедитесь, что он имеет расширение .csv и заполните некоторые данные. Здесь у нас есть CSV-файл, который содержит имена учеников и их оценки.

Creating a spreadsheet to generate a CSV

Ниже приведен код для чтения данных в нашем CSV с использованием функции csv.reader и класса csv.DictReader .

Чтение CSV-файла с помощью csv.reader

Источник

Оцените статью