How to Create a CSV-Formatted File with a Plain Text File

Programming Concepts



In this article, we show how to create a CSV-formatted file with a plain text file.

There are many ways of creating CSV files. They can be created with commercial software such as Microsoft Excel, Microsoft Access, and many other types of enterprise software.

However, a CSV file can also be created by something as simple as a plain text file by software such as notepad or wordpad (for Windows) or TextEdit (for Mac).

With a commercial software, you save the CSV file with a .csv file extension. However, when creating a CSV-formatted file with a plain text file, the file is saved simply as a text file (.txt). Even though it is simply a text file, as long as the data in it is formatted with comma-separated values, it will function as a CSV file.


Creating a CSV File

So to create a CSV file, open up a software that creates text files such as notepad or wordpad if you are on Windows. For a Mac computers, this would be TextEdit.

Create a new text file (.txt) and name it whatever you would like.

We've now created a file that will act as a CSV file so that we can transfer the data we put into it into database tables.

Now that we've created our file, we need to add the data in it, which must be formatted in CSV format.

To put data in CSV format, we simply put the data in the forms of rows and columns. Each new line in the text file represents a new row. Within each row, you place a comma to separate the various columns of the table.

So, say we have a table that represents office employees composed of a name column, an age column, and an occupation column.

We will add 2 employees data to the CSV file.

One of the employees is Vanessa, age 26, who is a secretary.

Another employee is Ron, age 25, who is a janitor.

To add this to the text file in CSV format, we have the following data in the text file, shown below.



You can see this CSV-formatted file at the following link: Office Employees CSV File In a Plain Text File.

The file is extremely simple.

We have 2 rows of data composed of 3 columns each. The first column is the name of the employee, the second column is the age of the employee, and the third column is the occupation of the employee at this office.

This is all that a CSV file must have. The rows representing the new sets of data that go into a database table and the the columns of each row which are separated by commas. So a comma acts as a delimiter separating the rows. This is why a CSV file is called a comma-separated values file.

With this simple CSV file, we can use it for various purposes. We can use it to be read by a software such as Microsoft Excel or we can use it to enter data into a MySQL database table or for various other software.

Again, this just goes to show you how easy it is to create a CSV file with a plain text file. And this same CSV file can be used across numerous different types of software.

To see how to load a CSV file into a MySQL table, see How to Load a CSV File into a MySQL Table Using PHP.


HTML Comment Box is loading comments...