In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Python to read CSV files, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
A typical dataset stocks.csv:
A stock data set is actually common tabular data. There are stock symbol, price, date, time, price change and trading volume. This data set is actually a tabular data with its own head and body.
The first trick: simple reading
Let's first look at a simple reading method. First, we use the csv.reader () function to read the handle f of the file to generate a handle of csv, which is actually an iterator. Let's take a look at the source code of this reader:
Feed reader an iterable object or the object of a file, and then return an iterable object.
First read the csv file, and then use csv.reader to generate a csv iterator f_csv
Then use the characteristics of the iterator, next (f_csv) to get the header of the csv file, that is, the header of the table data
Then, using the for loop, one line prints the contents of the row, that is, the body of the table data.
The second trick: use nametuple
The first trick above is actually the simplest. Let's wrap the generated row data with nametuple.
Nametuple is actually a very useful class, this class belongs to the collections module, and this module is simply a treasure chest with a lot of powerful libraries.
Here we use next (f_csv) to initialize the Row by actually getting the header of the table.
Then loop to construct the data of this Row and feed the data of each row in our table into row_info in nametuple format.
The advantage of this is that you can access the data in this row_info as you like, just like accessing class data, such as row_info.price.
The third trick: use tuple type conversion
If we are very clear about the type of each row of csv data, we can use a set data format conversion header to convert the data.
The steps of the operation are actually similar to those above, except that the cleaning of the data results is slightly different. Here is a very clever zip to construct a nested data list, and then use convert (data) to type the data of each line in the csv file, which is really a good trick!
Take a look at the results:
The fourth trick: use DictReader
In fact, the nametuple used above is also a data mapping. Is there any way to read the contents of csv directly with the mapping method and directly come up with a dictionary? there is, let's take a look at the code:
Is it very simple? the original csv module directly built-in DictReader (), read according to the dictionary method, and then generate an ordered dictionary, take a look at the results:
If you are interested, you can take a look at the source code of this DictReader (). It is actually an internally constructed iterator class, and the internal _ _ next__ is also generated by OrderedDict (zip (self.fieldnames, row)).
Fifth trick: convert with a dictionary
If we need to clean the data in this csv because it is read as a string and we need to update it to a specific data type, we can also use a dictionary to convert it, which is also very ingenious. Let's take a look at the source code:
The original data price Price and trading volume, I hope the final read is to generate a floating-point data and shaping data, so, with a dictionary to skillfully update key.
First we declare a custom type converter field_types
Then loop to generate an iterative object (key,conversion (row [key])
Finally, update the same key in the dictionary, for example, the contents of row ['price'] will be updated.
Thank you for reading this article carefully. I hope the article "how to read CSV files with Python" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.