Python
docs.python.org › 3 › library › csv.html
csv — CSV File Reading and Writing
These differences can make it annoying to process CSV files from multiple sources. Still, while the delimiters and quoting characters vary, the overall format is similar enough that it is possible to write a single module which can efficiently manipulate such data, hiding the details of reading and writing the data from the programmer.
W3Schools
w3schools.com › python › ref_module_csv.asp
Python csv Module
Built-in Modules Random Module Requests Module Statistics Module Math Module cMath Module · Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... import csv from io import StringIO data = "name,age\nEmil,14\nTobias,12" r = csv.reader(StringIO(data)) for row in r: print(row) Try it Yourself »
Library for csv file
Csv is already built into python. Don't use pandas unless you have a specific reason to. Especially if you're new to python. Also, you should use Google before posting questions. "Python csv" obviously informs you of the built in csv library and lots of examples https://docs.python.org/3/library/csv.html More on reddit.com
Working with csv files
In theory, you could. If you only have lines without \n, " and , (or whatever separator you have), it shouldn't be that difficult. But there is already the csv module, which is rather simple, so you might want to use that. More on reddit.com
How to read and write CSV files without using CSV module.
Sure. csv just stands for "comma separated value", so you'll be using things like .split to split a string on commas, .join for constructing the line to write back to the file, and things like basic file reading and writing. >>> line = 'data1,data2,data2' >>> line.split(',') ['data1', 'data2', 'data3'] >>> data = ['data1', 'data2', 'data3'] >>> ','.join(data) 'data1,data2,data3' You'll have to be careful about values that themselves contain commas or newlines (for example addresses) so check to be sure whether you have to worry about those in your project data or not. More on reddit.com
Need help decoding a csv file.
tl;dr: Your data file likely includes non-UTF-8 data, whether because it's a different encoding or due to corrupted/dirty records. You are likely hitting an error in your processing code after the CSV step, which you haven't shown us. That error is probably due to your (possibly implicitly) attempting to decode some data as UTF-8. You should fix this by (a) making sure you are decoding with the right encoding (e.g. UTF-8 vs. Latin-1 vs. Windows-1252, etc.) and (b) using try: ... except: ... to catch and properly deal with malformed records in your processing code. The solutions you have tried so far attempt to move the unicode decoding to before the csv processing, but the csv module requires raw bytes, not decoded characters, so don't help. You should continue to explicitly pass raw bytes to the csv module (using open("myfile.csv", "rb")), then immediately and explicitly attempt to decode the (raw, undecoded byte) strings in the data list, prior to applying your business logic to the data list. You will likely encounter these same errors you've been seeing here in this explicit decoding step, so you should expect and deal with them here. I'm posting based on details from below, but as a top level response because I'm addressing your general question. I don't think Python 3 will help you; the restriction on the CSV module still applies there. Further, one reason you're not seeing the error disappear when you mangle the data with io.open(..., errors="replace", ...) is that you're not actually attempting to process the mangled data; you're mangling the data, then throwing away the result, then reopening the raw file with the csv module: import io # you're not saving the file object returned by io.open io.open(csv, encoding="ascii", errors="replace") # you're now creating a new file object with codecs.open(...) data = [row for row in csv.reader(codecs.open (filename, 'U', encoding="ascii"))] Since you didn't actually use the "sanitized" data, you're of course still seeing the error. Now, I happen to think that raw destruction of accented/non-ASCII characters is a terrible approach; I wouldn't use io.open(..., errors="replace",...) unless I absolutely had to. However, that's still not the solution; it's just a debugging of the buggy solution u/willm gave you. Here's the problem: The csv module wants raw byte string inputs, not Unicode strings. Using io.open(..., encoding="...", ...) gives you a file reader that returns Unicode strings, which is precisely what you don't want. Actually, this is also a problem with codecs.open(..., encoding=...); that lets you open a file and automatically decode the inputs to Unicode strings using the encoding you specify. Again, since csv needs raw bytes strings, this is not what you want. Incidentally, csv shouldn't want raw byte strings; it must operate on the contents of the files, which must be decoded in some fashion for this to work (for instance, what if the comma character were not encoded using the same byte value in some obscure text encoding; then the csv module wouldn't be able to see the delimiters!). This is a limitation of the module, and is why there is a warning in the docs. So your codecs.open(...) approach was a good idea, but csv isn't ready for it yet. So, back to why you have a problem in the first place. Let's start with your initial attempt: data = [row for row in csv.reader(open (os.path.expanduser("~/Downloads/" + filename),'U'))] And break down the list comprehension: data = [] file_path = os.path.expanduser("~/Downloads/" + filename) file_object = open(file_path, "U") reader = csv.reader(file_object) for row in reader: data.append(row) Ok, much clearer. One thing to note is that's it's conceivable that you'll one day have a unicode filename, which could cause trouble on my line 2 when you construct the file path. Since you're processing some but not all rows of the file, though, that's not what's happening here. Next, you're opening the file in text mode using the "U" flag to open(). Don't do this; open the file as binary, and let the csv module figure out the line endings: ... file_object = open(file_path, "rb") ... What will happen now is that csv will deal with the raw bytes of your file, and return lists filled with raw byte strings, not friendly Unicode strings of characters. Now, this is still the same as what "U" was doing, just a bit more explicit. i.e. Your initial program behaved exactly the same way, returning byte not Unicode strings. What does this mean? My guess is that the Unicode error was not thrown on the data = ... line, but later on in your program, as you attempt to utilize the returned byte strings. Can you show us more of your processing of the data object? My suspicion is that somewhere you're doing something equivalent to unicode("some data field with broken utf-8 \xe8", "utf8"), and it's throwing a decode error when it hits invalid UTF-8 data. If this is the case, you need to guard against malformed UTF-8 data by catching exceptions, and figuring out how to process damaged data records. Alternatively, your file may not be encoded in UTF-8! One hint that this may be the case is that the offending byte, 0xe8, is 'a' circumflex ("â") when encoded in ISO-8859-1 ("Latin-1"). In this case, you'd switch to unicode("some data field with latin-1 \xe8", "latin1"), and everything would be fine. Note that if your file is Latin-1 encoded, there will be no decode errors; every byte is valid Latin-1, so mangled data will just show inappropriate characters, but Python will not be able to auto-detect that it's mangled and throw an exception. This may seem convenient, but it's a bad thing, overall – you'd usually rather have an exception tell you the data's bad, rather than use bad data silently. Here are a few tests to run that get rid of the CSV processing complexity: # silent if file is good UTF-8, exception if it's not UTF-8 or is broken with open("myfile.csv", "rb") as f: unicode(f.read(), "utf-8") # prints whole file, assuming your OS can handle UTF-8 data printing # (you're good on Mac/Linux). Look through the data manually; accented # characters should be correct. If instead you see multiple junk chars # in place of an accented char, your file is probably not Latin-1 with open("myfile.csv", "rb") as f: print unicode(f.read(), "latin-1") Finally, note that the codecs.open(...) solution you tried moved the Unicode decode issue, because it attempted to convert the presumably broken UTF-8 data up front, before you did your CSV processing, whereas your initial program attempted to decode the data on a per-record basis, after the CSV processing. More on reddit.com
10:57
Python CSV Tutorial | Read and Write CSV Files - YouTube
06:35
Learn HOW to Read CSV Files in Python! - YouTube
04:14
Reading CSV Files With Python's csv Module - YouTube
16:12
Python Tutorial: CSV Module - How to Read, Parse, and Write CSV ...
04:03
CSV Module Python Programming Basics For Beginners #29 - YouTube
18:21
How to Read and Write CSV Files in Python - YouTube
Python Beginners
python-adv-web-apps.readthedocs.io › en › latest › csv.html
CSV Files — Python Beginners documentation - Read the Docs
Import the module. Open a file for writing. Create a CSV writer object and assign it to a new variable. Write the header row into the CSV. Write all the other rows into the CSV. Normally this will involve a for-loop. Close the file. The following example script uses a CSV file named presidents.csv.
GeeksforGeeks
geeksforgeeks.org › python › working-csv-files-python
Working with csv files in Python - GeeksforGeeks
The above example uses a CSV file aapl.csv which can be downloaded from here . ... Loop through csvreader to append each row (as a list) to rows. Print total rows, headers and first 5 data rows in a formatted view. We can read a CSV file into a dictionary using the csv module in Python and the csv.DictReader class.
Published August 5, 2025
ZetCode
zetcode.com › python › csv
Python CSV - read, write CSV in Python
January 29, 2024 - To use Python CSV module, we import csv. The csv.reader method returns a reader object which iterates over lines in the given CSV file. ... The numbers.csv file contains numbers. ... #!/usr/bin/python import csv with open('numbers.csv', 'r') as f: reader = csv.reader(f) for row in reader: for ...
Python Module of the Week
pymotw.com › 2 › csv
csv – Comma-separated value files - Python Module of the Week
This example file was exported from NeoOffice. "Title 1","Title 2","Title 3" 1,"a",08/18/07 2,"b",08/19/07 3,"c",08/20/07 4,"d",08/21/07 5,"e",08/22/07 6,"f",08/23/07 7,"g",08/24/07 8,"h",08/25/07 9,"i",08/26/07 · As it is read, each row of the input data is parsed and converted to a list of strings. $ python csv_reader.py testdata.csv ['Title 1', 'Title 2', 'Title 3'] ['1', 'a', '08/18/07'] ['2', 'b', '08/19/07'] ['3', 'c', '08/20/07'] ['4', 'd', '08/21/07'] ['5', 'e', '08/22/07'] ['6', 'f', '08/23/07'] ['7', 'g', '08/24/07'] ['8', 'h', '08/25/07'] ['9', 'i', '08/26/07']
DEV Community
dev.to › devasservice › guide-to-pythons-csv-module-32ie
Guide to Python's CSV Module - DEV Community
October 10, 2024 - In this example, QUOTE_ALL ensures that every field is wrapped in quotes. Other quoting options include csv.QUOTE_MINIMAL, csv.QUOTE_NONNUMERIC, and csv.QUOTE_NONE, giving you full control over how your CSV data is formatted. Over the years, I’ve come to rely on the CSV format as a lightweight, efficient way to move data around, and Python’s csv module has been a trusty companion in that journey.
Developer-service
developer-service.blog › guide-to-pythons-csv-module
Guide to Python's CSV Module
October 10, 2024 - In this article, I’ll break down ... works in Python, from basic usage to more advanced techniques that can save you tons of time when processing data. Before diving into the csv module, let’s start with a basic understanding of what a CSV file is. A CSV file is essentially a plain text file where each line represents a row of data, and each value is separated by a comma (or sometimes other delimiters like tabs). Here's a quick example of what it ...
Programiz
programiz.com › python-programming › csv
Python CSV: Read and Write CSV files
Name, Age, Profession Jack, 23, Doctor Miller, 22, Engineer · Now, let's read this csv file. import csv with open('people.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row) ... We then used the csv.reader() function to read the file.
PythonForBeginners
pythonforbeginners.com › home › using the csv module in python
Using the CSV module in Python - PythonForBeginners.com
August 25, 2020 - In the first two lines, we are importing the CSV and sys modules. Then, we open the CSV file we want to pull information from. Next, we create the reader object, iterate the rows of the file, and then print them. Finally, we close out the operation. We’re going to take a look at an example CSV file. Pay attention to how the information is stored and presented. Title,Release Date,Director And Now For Something Completely Different,1971,Ian MacNaughton Monty Python And The Holy Grail,1975,Terry Gilliam and Terry Jones Monty Python's Life Of Brian,1979,Terry Jones Monty Python Live At The Hollywood Bowl,1982,Terry Hughes Monty Python's The Meaning Of Life,1983,Terry Jones
LabEx
labex.io › home › modules › csv module
Python CSV Module - Python Cheat Sheet
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.
Codecademy
codecademy.com › article › python-csv-file
Python CSV Tutorial: Read, Write, and Edit CSV Files | Codecademy
The following code opens the example.csv file using the open() function and with read mode. Then we use the next() function to skip the header row and a for loop to iterate over each row in the CSV file: ... Now that we know how to open a CSV file, let’s dive into how we can read its contents using Python’s csv module.