1. Use Python basic syntax to read and write CSV files
The general idea of reading the data in the CSV file with basic syntax is: get the file object, read the table header, split the header field according to the comma separator, use the for loop statement to get the record data of the table body, and write it into another file after splitting In a CSV file (if you want to write data into a file in xls* format, please refer to the previous official account article), the steps are as follows:
Step 1: Import the necessary modules to obtain the input and output file paths.
import sys
infile = sys.argv[1]
outfile = sys.argv[2]
Step 2: Use the open built-in function to get the file object.
with open(infile, “r”, newline=‘’) as fr, open(outfile, “w”,
newline=‘’) as fw:
Step 3: Use the readline method of the file object or the next method of the iterator to read the table header (the file object is an iterator object that supports the iteration protocol), and use the str.split method to split the table header (note, use strip function to remove the trailing newline).
header = next(fr)
header = header.strip()
header_list = header.split(“,”)
Step 4: Now that the table header has been checked and divided, we need to write the table header into the file object (note that a newline character needs to be added at the end).
fw.write(“,”.join(map(str, header_list)) + “\n”)
Of course, the above code can also be written like this:
print(*header_list, sep=“,”, file=fw)
Step 5: Use the for loop to read the table body data, split it into a list and write it to the file.
for row in fr:
row = row.strip()
row_list = row.split(“,”)
fw.write(“,”.join(map(str, header_list)) + “\n”)
After the above steps are completed, enter in the command prompt:
python csvrw.py inputfile.csv outputfile.csv
Please customize the input and output csv file names and script names, the above are just examples.
The above code is as follows:
Use pandas to read and write CSV files
2. Use pandas to read and write CSV files
The pandas library is a powerful data processing and data analysis library. It is easier to use pandas to process csv files. The steps are as follows:
Step 1: First, import the necessary modules to obtain the input and output file paths.
import sys
import pandas as pd
infile = sys.argv[1]
outfile = sys.argv[2]
Step 2: Use the read_csv method of pandas to store the data into a DataFrame object.
dataframe = pd.read_csv(infile)
Step 3: Then use the to_csv method of DataFrame to output it to another csv table.
dataframe.to_csv(outfile, index=False)
After the above steps are completed, enter the corresponding command in the command prompt (please refer to <2>)
code show as below:
As an IT veteran, what I share are some of my own learning experiences and dry goods.
1. Learning routes in all directions of Python
The technical points in all directions of Python are sorted out to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you can learn more comprehensively.
2. Essential development tools for Python
3. Excellent Python learning books
When I have learned a certain foundation and have my own understanding ability, I will read some books or handwritten notes compiled by my predecessors. These notes record their understanding of some technical points in detail. These understandings are relatively unique and can be learned. to a different way of thinking.
4. Python video collection
Watching the zero-based learning video is the fastest and most effective way to learn. Following the teacher's ideas in the video, it is still very easy to get started from the basics to the in-depth.
5. Practical cases
Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.
6. Python exercises
Check the learning results.
7. Interview information
We must learn Python to find high-paying jobs. The following interview questions are the latest interview materials from first-line Internet companies such as Ali, Tencent, and Byte, and Ali bosses have given authoritative answers. After finishing this set The interview materials believe that everyone can find a satisfactory job.
This full version of the full set of learning materials for Python has been uploaded to CSDN. If you need it, you can scan the QR code of CSDN official certification below on WeChat to get it for free [100% free guarantee]