Detailed Explanation of python File Processing
- 2021-12-09 09:26:22
- OfStack
File operation
This is my notes in the process of learning python, which will be updated continuously. Please ask questions and correct me.
1. txt file
1. Text file txt
2.2 Binary files, pictures and videos
Operation Flow Open-Operation-Close
Open a file
Variable name = open (file path and file name, schema)
Operation file
Variable name. close
Path uses\\ or/
模式 | 描述 |
---|---|
r | 只读 |
w | 覆盖写, |
x | 创建写,创建文件 |
a | 追加写 |
b | 2进制文件模式 |
t | 文本文件模式 |
+ | 与r/w/x/a1同使用 |
There is a problem in the writing position in the test, which only appeared once. I don't know if it is the problem of my operation. I hope to know the big brother can give directions under 1
f=open("G:\\Python\\txt.txt","a")
f=open("G:\\Python\\txt.txt","r+")
print(f.read())
f.write("99999999")
f.close()
# File reading is only the first 1 It will be called next time, and it will not take effect later
# Measured a+ Unable to read file, r+ Cursor appears at the beginning in mode 1 Times
read(10) Read 10 Characters
readline() Read the 1 Row, before reading the row with parameters n Characters
readlines() Read all rows, and if there are parameters, read the n Row
seek ( 0 ) Change the pointer position, 0 For the beginning, 1 For the end
write () Writes content to file
writelines () Joins all elements of a list type and writes them to a document
2. Data dimension
1.1 Dimensional data
It is composed of ordered and unordered data of peer-to-peer relationship
2.2 Dimensional data
Also known as tabular data, it is organized in a two-dimensional tabular way, corresponding to the matrix in mathematics
3. High-dimensional data
It is composed of key-value pair type data, organized by object mode, and can be nested in multiple layers
High-dimensional data is common in web, and is the main way of organizing content in internet, including HTML, XML, JSON and so on
HTML: Available on Web page F12
XML: Android Studio
JSON: https://daily.zhihu.com/
https://news-at.zhihu.com/api/3/stories/latest
1-bit data
It is mainly stored in the form of list in python
The general idea is to use special characters to separate elements, such as spaces, commas, line breaks, and other special separators
ls=['1','2','3']
f.write(",".jion(ls))
join() Connecting elements in a sequence with specified characters generates a new string
print(",".jion(ls))
csv File Comma Separated Values
A generic, relatively simple file format with a suffix of. csv
After 1-bit data is saved, the elements are separated by commas (English commas), which is widely used in business and science
f=open("12.csv","r")
ls=f.read()
ls_new=ls.split(',')
# Slice the string by specifying the delimiter and return the list
f.close
2D data
csv files can also store 2-dimensional data
ls=[["1","2"],
["3",["4"]]]
f=open("12.csv","w")
for row in ls:
f.write(",".join(row)+"\n")
f.close
f = open("12.csv", "r")
ls = []
for line in f:
ls.append(line.strip('\n').split(","))
# a=line.strip('\n')
# b=a.split(',')
# ls.append(b)
f.close
3. Excel file
# Introduce Excel Library xlrd 1.20 Version
import xlrd
# Open the one we just wrote test_w.xls Documents
wb = xlrd.open_workbook(r'E:\MATLAB\project\shiyan\12.xlsx')
# Get and print sheet Quantity
print( "sheet Quantity :", wb.nsheets)
# Get and print sheet Name
print( "sheet Name :", wb.sheet_names())
# According to sheet Index to get content
sh1 = wb.sheet_by_index(0)
# It can also be based on sheet Name get content
sh = wb.sheet_by_name(' Achievement ')
# Gets and prints the sheet Number of rows and columns
print( u"sheet %s Altogether %d Row %d Column " % (sh1.name, sh1.nrows, sh1.ncols))
# Gets and prints the value of a cell
print( " No. 1 1 Line number 2 The value of the column is :", sh1.cell_value(0, 1))
# Gets the value of an entire row or column
rows = sh1.row_values(0) # Get the 1 Line content
cols = sh1.col_values(1) # Get the 2 Column content
# Print the obtained column and column values
print( " No. 1 1 The value of the row is :", rows)
print( " No. 1 2 The value of the column is :", cols)
# Gets the data type of the cell content
print( " No. 1 2 Line number 1 The value type of the column is :", sh1.cell(1, 0).ctype)ets()[0]
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!