Introduction to this chapter
What is data formatting
Preface:
-After studying this chapter, you will have a standardized/formatted perspective on data
-Methodology: Understanding files and data representation from a Python perspective
-Practical ability: Learn to write programs with file input and output
1. Dimensions of data organization
Dimension: The organization of a set of data-linear or two-dimensional or higher
One-dimensional data: It is composed of ordered or disordered data in a peer-to-peer relationship, organized in a linear manner
Two-dimensional data: It is composed of multiple one-dimensional data, which is a combination of one-dimensional data (a table is a typical two-dimensional data, and the header is a part of the two-dimensional data)
Multi-dimensional data: the expansion of one-dimensional or two-dimensional data in a new dimension
2. Data operation cycle
Storage <-> means <-> operation-a total of three stages
3. Representation of one-dimensional data
Ordered, use list
Unordered, use set
Both lists and collections can be used to traverse data with for
4. Storage of one-dimensional data
Method 1: Use one or more spaces to separate, without line breaks,
such as: China, the United States, Japan, Italy
Restrictions: no spaces can exist in the data
Method 2: Use English half-width commas to separate without line breaks,
such as: China, the United States, Japan, and Italy.
Restrictions: English commas cannot be included in the data
Other methods: use special symbols to separate
5. One-dimensional data processing
Example 1
f = open("E:/code/python/821/fname.txt",encoding="utf-8")
txt = f.read()
ls = txt.split()
f.close()
print(ls)
输出
['中国', '美国', '日本', '意大利', '德国', '法国', '英国', '意大利']
Example 2
ls = ['新疆','北京','大连']
f = open("E:/code/python/821/fname.txt","w",encoding="utf-8")
f.write(" ".join(ls))
f.close()
View the file at this time
Summary: read-split() write-join()
6. Representation of two-dimensional data
Generally use a two-dimensional list-itself and each element in the list are lists
Use a two-level for loop to traverse each element
7. CSV format and 2D data storage
CSV: Comma-Separated Values-Comma-separated values
-internationally used one and two-dimensional data storage format, general .csv extension
-one one-dimensional data per line, separated by commas, no blank lines-
Excel and general editing software can be used Read or save as a .csv file
Convention
-if an element is missing, the comma still needs to be tabulated
-the header of the two-dimensional data can be stored as data or stored separately
If an element contains a comma, you need to add quotation marks on both sides of the element, indicating that the comma is not used to separate the element
General indexing habits: first row and then column ls[row] column
8. Two-dimensional data processing
Example 1
fo = open(fname)
ls = []
for line in fo:
line = line.replace("\n","")#根据每行结束都有回车的习惯
ls.append(line.split(","))#括号内得到由,分隔开的列表
fo.close()
Example 2 Write data to a file in CSV format
ls = [[],[],[]] #二维列表
f = open(fname,'w')
for item in ls:
f.write(','.join(item) + "\n")
f.close()
Example 3 Traverse
ls = [[1,2],[3,4],[5,6]] #二维列表
for row in ls:
for column in row:
print(column)
输出
1
2
3
4
5
6
Source: BIT Python MOOC