From 50a45d30942831533534b447d6316159f4429dc3 Mon Sep 17 00:00:00 2001 From: mothatceguy Date: Tue, 26 Aug 2025 20:58:36 -0300 Subject: [PATCH 1/3] Add Csv module documentation and update contributors list --- contributors/contributors.json | 3 +- docs/modules/csv-module.md | 146 +++++++++++++++++++++++++++++++++ src/store/navigation.ts | 4 + 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 docs/modules/csv-module.md diff --git a/contributors/contributors.json b/contributors/contributors.json index 8dca2138..a597bf77 100644 --- a/contributors/contributors.json +++ b/contributors/contributors.json @@ -81,5 +81,6 @@ "pyxfluff", "pranayat", "prashanthbhat203", - "QuarkZ26" + "QuarkZ26", + "manuothatceguy" ] \ No newline at end of file diff --git a/docs/modules/csv-module.md b/docs/modules/csv-module.md new file mode 100644 index 00000000..cfa35283 --- /dev/null +++ b/docs/modules/csv-module.md @@ -0,0 +1,146 @@ +--- +title: Python Csv Module - Python Cheatsheet +description: Python has a csv module, which allows us to work easily with CSV files. +--- + + +Python Csv Module + + +The csv module provides tools to read from and write to CSV files, which are commonly used for data exchange. + + + + csv Module vs Manual File Read-Write + + + While you can read and write CSV files using basic file operations and string methods (like open() with read or write and split()), the csv module is designed to handle edge cases such as quoted fields, embedded delimiters, and different line endings. It ensures compatibility with CSV files generated by other programs (like Excel) and reduces the risk of parsing errors. For most CSV tasks, prefer the csv module over manual parsing. +
+ For more on file handling basics, see the File and directory Paths page. +
+
+ +To get started, import the module: +```python +import csv +``` + +## csv.reader() + +This function receives a file which needs to be an [iterable of strings](https://docs.python.org/3/library/csv.html#id4:~:text=A%20csvfile%20must%20be%20an%20iterable%20of%20strings%2C%20each%20in%20the%20reader%E2%80%99s%20defined%20csv%20format). In other words, it should be the open file as it follows: + +```python +import csv + +file_path = 'file.csv' + +with open(file_path, 'r', newline='') as csvfile: + reader = csv.reader(csvfile) + + for line in reader: + print(line) +``` + +This function returns a reader object which can be easily iterated over to obtain each row. Each column in the corresponding rows can be accessed by the index, without the need to use the built-in function [`split()`](https://www.pythoncheatsheet.org/cheatsheet/manipulating-strings#split). + +## csv.writer() +This function receives the file to be written as a csv file, similar to the reader function, it should be invoked as this: + +```python +import csv + +file_path = 'file.csv' + +with open(file_path, 'w', newline='') as csvfile: + writer = csv.writer(csvfile) + + # do something +``` + +The "do something" block could be replaced with the use of the following functions. +### writer.writerow() +Writes a single row to the CSV file. + +```python +writer.writerow(['name', 'age', 'city']) +writer.writerow(['Alice', 30, 'London']) +``` + +### writer.writerows() +Writes multiple rows at once. + +```python +rows = [ + ['name', 'age', 'city'], + ['Bob', 25, 'Paris'], + ['Carol', 28, 'Berlin'] +] +writer.writerows(rows) +``` + +## csv.DictReader + +Allows you to read CSV files and access each row as a dictionary, using the first row of the file as the keys (column headers) by default. + +```python +import csv + +with open('people.csv', 'r', newline='') as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + print(row['name'], row['age']) +``` + +- Each `row` is an `OrderedDict` (or a regular `dict` in Python 3.8+). +- If your CSV does not have headers, you can provide them with the `fieldnames` parameter: + ```python + reader = csv.DictReader(csvfile, fieldnames=['name', 'age', 'city']) + ``` + +## csv.DictWriter + +Lets you write dictionaries as rows in a CSV file. You must specify the fieldnames (column headers) when creating the writer. + +```python +import csv + +fieldnames = ['name', 'age', 'city'] +rows = [ + {'name': 'Alice', 'age': 30, 'city': 'London'}, + {'name': 'Bob', 'age': 25, 'city': 'Paris'} +] + +with open('people_dict.csv', 'w', newline='') as csvfile: + writer = csv.DictWriter(csvfile, fieldnames=fieldnames) + writer.writeheader() # writes the header row + writer.writerows(rows) +``` + +- Use `writer.writeheader()` to write the column headers as the first row. +- Each dictionary in `writer.writerows()` must have keys matching the `fieldnames` specified when creating the writer. + +## Additional params to csv.reader() and csv.writer() + +### delimiter +Should be the character used to separate the fields. As the file type says, the default is the comma ','. Depending on the locale, Excel might generate csv files with the semicolon as a delimiter. + +```python +import csv + +with open('data_semicolon.csv', newline='') as csvfile: + reader = csv.reader(csvfile, delimiter=';') + for row in reader: + print(row) +``` + +### lineterminator +Character or sequence of characters to end a line. Most common is "\r\n" but it could be "\n". + +### quotechar +Character used to quote fields containing special characters (default is `"`). + +```python +reader = csv.reader(csvfile, quotechar="'") +``` + +For more details, see the official Python csv module documentation. \ No newline at end of file diff --git a/src/store/navigation.ts b/src/store/navigation.ts index 7e123519..3aaf397a 100644 --- a/src/store/navigation.ts +++ b/src/store/navigation.ts @@ -149,6 +149,10 @@ export const useNavigationStore = defineStore('navigation', { path: '/modules/copy-module', updated: false, }, + { + name: 'Csv', + path: '/modules/csv-module' + }, { name: 'Datetime', path: '/modules/datetime-module', From d80c4a47702a3721af4c0443bfebe8667d886cab Mon Sep 17 00:00:00 2001 From: mothatceguy Date: Tue, 26 Aug 2025 21:02:08 -0300 Subject: [PATCH 2/3] fix: typo --- docs/modules/csv-module.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/modules/csv-module.md b/docs/modules/csv-module.md index cfa35283..69ac38b8 100644 --- a/docs/modules/csv-module.md +++ b/docs/modules/csv-module.md @@ -57,7 +57,7 @@ with open(file_path, 'w', newline='') as csvfile: # do something ``` -The "do something" block could be replaced with the use of the following functions. +The "do something" block could be replaced with the use of the following functions: ### writer.writerow() Writes a single row to the CSV file. From 791332a69f2b58eae064ca71cb82605667435ecb Mon Sep 17 00:00:00 2001 From: mothatceguy Date: Tue, 26 Aug 2025 21:12:46 -0300 Subject: [PATCH 3/3] fix: add missing line breaks for better readability in csv-module documentation --- docs/modules/csv-module.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/modules/csv-module.md b/docs/modules/csv-module.md index 69ac38b8..f6f8e34b 100644 --- a/docs/modules/csv-module.md +++ b/docs/modules/csv-module.md @@ -44,6 +44,7 @@ with open(file_path, 'r', newline='') as csvfile: This function returns a reader object which can be easily iterated over to obtain each row. Each column in the corresponding rows can be accessed by the index, without the need to use the built-in function [`split()`](https://www.pythoncheatsheet.org/cheatsheet/manipulating-strings#split). ## csv.writer() + This function receives the file to be written as a csv file, similar to the reader function, it should be invoked as this: ```python @@ -59,6 +60,7 @@ with open(file_path, 'w', newline='') as csvfile: The "do something" block could be replaced with the use of the following functions: ### writer.writerow() + Writes a single row to the CSV file. ```python @@ -67,6 +69,7 @@ writer.writerow(['Alice', 30, 'London']) ``` ### writer.writerows() + Writes multiple rows at once. ```python @@ -122,6 +125,7 @@ with open('people_dict.csv', 'w', newline='') as csvfile: ## Additional params to csv.reader() and csv.writer() ### delimiter + Should be the character used to separate the fields. As the file type says, the default is the comma ','. Depending on the locale, Excel might generate csv files with the semicolon as a delimiter. ```python @@ -134,9 +138,11 @@ with open('data_semicolon.csv', newline='') as csvfile: ``` ### lineterminator + Character or sequence of characters to end a line. Most common is "\r\n" but it could be "\n". ### quotechar + Character used to quote fields containing special characters (default is `"`). ```python