CSV File
Learn what a CSV file is and how to read it in Powershell and Python.
Introduction
Exchange of information into and out of programs is a requirement for most programming and scripting languages worldwide. The most common approach for this information exchange is through text files. One of the most popular formats for exchanging such data is the CSV format.
What is CSV?
CSV is short for Comma-Separated Values, which is tabular data that has been saved as plaintext data separated by commas (,
) or any other delimiter. For example, we have data in the following table that has header and data elements. This data can be converted into CSV data where each row of a table is a new line and has data separated with a comma.
Tabular Data:
Column1 | Column2 | Column3 |
---|---|---|
Data1 | Data2 | Data3 |
Data1 | Data2 | Data3 |
CSV Data:
Column1,Column2,Column3
Data1,Data2,Data3
Data1,Data2,Data3
Reading a CSV File
Let’s take a look at how to read CSV files.
Using PowerShell cmdlet Import-CSV
Reading CSV data from a file is fairly easy in PowerShell. All we need to do is use the Import-CSV
cmdlet with a valid file path. PowerShell will read the CSV file and convert it to PowerShell objects and return objects on our console. Let’s suppose we have the following CSV data saved as filename Service.csv
.
"Name","Status"
"WinDefend","Stopped"
"WinHttpAutoProxySvc","Running"
"Winmgmt","Running"
"WinRM","Running"
To read or parse the CSV formatted files into PowerShell objects, run Import-CSV <File Name>
.
Import-Csv C:\Temp\Service.csv
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy