Python writes the ninety-nine multiplication table and writes it into the text file exercise7_1.txt

First, code the idea according to the effect you want

1. First create a new exercise7_1.txt file on the F disk

2. Conceive the code

is to achieve:

insert image description here
insert image description here
Set the first number of the multiplication formula to i, and the second number to j, and it can be found that each row is to increase i to be equal to j,
so the code:

with open('f:\\exercise7_1.txt','r+') as f:
    for j in range(1,10):#j的范围是1-9
        for i in range(1,j+1):#i的范围是增大到和j相等
            f.write(str(i)+'*'+str(j)+ '=' + str(i*j) + '\t')
        f.write('\n')#写完一行要换行

In addition: Common read and write operations:

with open(r'filename.txt') as f:
data_user=pd.read_csv(f) #File read operation

with open('data.txt', 'w') as f:
f.write('hello world') #file write operation

Related parameters:

r: Open the file for reading only. The file pointer will be placed at the beginning of the file. This is the default mode .
rb: Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
r+: Open a file for reading and writing. The file pointer will be placed at the beginning of the file.
rb+: Open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file.
w: Open a file for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb: Open a file in binary format for writing only. Overwrite the file if it already exists. If the file does not exist, create a new file.
w+: Open a file for reading and writing. Overwrite the file if it already exists. If the file does not exist, create a new file.
wb+: Open a file in binary format for reading and writing. Overwrite the file if it already exists. If the file does not exist, create a new file.
a: Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.
ab: Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.
a+: Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. The file will be opened in append mode. If the file does not exist, create a new file for reading and writing.
ab+: Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing.

Properties of the file object:

file.read([size]) returns the file data as a string, the optional parameter size controls the number of bytes read
file.readlines([size]) returns a list of the line content in the file, the size parameter is optional
file.write( str) Write the string to the file
file.writelines(strings) Write the sequence of strings to the file
file.close() Close the file
file.closed means the file has been closed, otherwise False

file.mode The access mode used when the Access file is opened
file.encoding The encoding used by the file
file.name The file name
file.newlines is None when no line separator is read, and a string when there is only one line separator. When the file has multiple types of line endings, it is a list file.softspace that contains all the currently encountered line endings.
0 means that a space character should be added after outputting a data, and 1 means not added. This attribute is generally not used by programmers, it is used internally by the program

Guess you like

Origin blog.csdn.net/qq_46161529/article/details/121441751