Hi. Doesn't sound like a Django question, but I assume you came across this 
question when writing Django codes:) So anyway, let me try to see whether I 
could provide any useful information here.

This question is a bit too broad, so let me try to answer it in several 
levels.

First, python doesn't care the format of any file. From python's point of 
view, any file is just a sequence of binary data. On the lowest level, 
python only two types of operations: read N bytes from the file starting 
from a position; write N bytes to the file starting from a position. That's 
it. What each byte mean is up to the programmers. So on the level of python 
programming, there's no difference between a txt file and a csv file.

Having said the first point, there are standard formats defined by 
programmers, to make communication easier. First, among all binary files, 
there's special type of files called "text files". It's special because 
every byte of a text file must be a value from a certain table of symbols 
(think of the ASCII table). Not all files are text files. But if a file is 
a text file, then most text editors can handle that file well. (Python also 
have native support on the language level, that is, you open a file in the 
text mode if you use "open(<file name>, 'r')" intead of "open(<file name>, 
'rb')". However, this is only for sake of convenience of human readability. 
It doesn't make a text file fundamentally different from other files. In 
fact, you can open a text file in the binary mode using 'rb'. Python will 
simply forget that the file is a text file.

Next, text files still form a big family. By assigning meanings to 
different symbols and creating rules of how the content should look like, 
there are more types under text files. csv is a special type of text file. 
(Example of othe types are .py, .html, etc). Since it's just a text file, 
you can open a csv file using "open(<csv file name>, 'r')" and do 
read/write operations like any other text file. However, since it has its 
own rules, there are also some helper libraries to assist you handling csv 
files, too. Examples are "csv" package, "pandas" package, etc. But again, 
since it's a text file, you can ignore those packages and write your codes 
using string functions directly. For example:

```
s = open("sample.csv", 'r').read()
lines = s.split('\n')
first_line = lines[0]
columns_lables = first_line.split(',')
......
```

Such a piece of codes will handle a very simple and standard csv file 
already. However, csv has more complications than this. It even has 
different variations (called dialets). If treating a csv file as an 
ordinary text file, you are basically re-inventing the wheel. (There's 
nothing wrong with that. Just tedious.) Those standard packages can handle 
those staff and let you focus on your business logic. That's all.

In the end, I want to emphasize that files' extension names doesn't mean 
anything. When I said a csv file above, I don't mean a file whose file name 
ends with ".csv". I mean a file whose content complies the csv rules. By 
creating a file called "something.csv", I'm merely giving a promise to 
anyone who open it that "I promise you that the content in this file 
satisfies the csv rules. You have my words!" Whether I keep my promise or 
not is totally up to me (indeed, I can rename an image file "pic1.jpg" to 
"pic1.csv".) In addition, you can save some csv data into a .txt files. For 
example, consider the following data:

Name,Job Title,Phone Number
Alice,Manager,23121212
Bob,Director,12345678
Chales,Developer,41212113

You can store the data into "contacts.txt". I will still call it a csv file 
even though it has ".txt" extension. Therefore, I can handle this file 
using any csv package. For example:

```
import pandas as pd
df = pd.read_csv("contacts.txt")
...
```

Pandas will not complain just because its extension is not ".csv". However, 
it will be a good practice to choose the extension which best describes the 
nature of the file's contents. In this way, you'll bring less confusion to 
the users of this file (including the future yourself).



On Saturday, July 18, 2020 at 2:25:57 PM UTC+8, Sead Sejo Gicić wrote:
>
> What is the difference between ordinary txt file and csv file for using in 
> Python programming?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7a9b233b-a61b-4866-8383-44b4b60ac736o%40googlegroups.com.

Reply via email to