03---python foundation (3) (file operation--function first acquaintance)

file operations

1, file path
2, encoding method: utf-8.
3, action mode, read, read-write, write-read.

f1 = open('D:\a.txt', encoding='utf-8', mode='r')
content = f1.read()
print(content)
#f1,文件句柄,文件对象,file,f_handle,file_handle,f_obj open打开的指令,windows的指令,
#windows 默认编码方式gbk,linux默认编码方式utf-8,mac utf-8。
1,打开文件,产生文件句柄。
2,操作文件句柄。
3,关闭文件。
#SyntaxError: (unicode error) 'unicodeescape' codeccan't decode bytes in position 2-3: truncated \UXXXXXXXX escape
 f1 = open(r'D:\a.txt', encoding='utf-8', mode='r') #有绝对路径文件前面写上 r 代表转义
 f1 = open('D:\\a.txt', encoding='utf-8',mode='r')  #绝对路径不写 r  必须在路径D:\a.txt加上\来转义
EncodeDecodeErorr: 编码错误。

r mode—read

f1 = open('D:\a.txt', encoding='utf-8', mode='r')  #默认为都模式  mode=r 可不写
content = f1.read()
print(content)
f1.close()

read : read all

f1 = open('log1', encoding='utf-8')
content = f1.read()  
print(content)
f1.close()

read(n) : r mode reads character by character

f1 = open('log1', encoding='utf-8')
content = f1.read(5)  # r 模式 按照字符读取。
print(content)    #更加快乐的
f1.close()

rb mode – read byte by byte

f1 = open('log1', mode='rb')
content = f1.read(3)  # rb模式 按照字节读取。
print(content.decode('utf-8')) #更  三个字节等于一个中文字符
f1.close()

readline() - read line by line

f1 = open('log1', encoding='utf-8')
print(f1.readline())
print(f1.readline())
print(f1.readline())
print(f1.readline())
f1.close()

#更加快乐的附件
#fdsaklf;jdsaf
#fdsakjfdsaf 多岁的发生
#fd66666666

readlines() - treat each line as an element of a list and return the list

f1 = open('log1', encoding='utf-8')
print(f1.readlines())  #['更加快乐的附件\n', 'fdsaklf;jdsaf\n', 'fdsakjfdsaf 多岁的发生\n', 'fd66666666\n', '6666']
f1.close()

for loop

f1 = open('log1', encoding='utf-8')
for i in f1:
    print(i)
f1.close()
#更加快乐的附件
#fdsaklf;jdsaf
#fdsakjfdsaf 多岁的发生
#fd66666666
#6666

decode: decoded into unincod
encode: specified to be parsed into utf-8, gbk encoding display

f2 = open('log1',mode='rb')
print(f2.read())
f2.close()
#编码的补充:\
s1 = b'\xd6\xd0\xb9\xfa'
s2 = s1.decode('gbk')
s3 = s2.encode('utf-8')
print(s3)  # b'\xe4\xb8\xad\xe5\x9b\xbd'
s1 = b'\xd6\xd0\xb9\xfa'.decode('gbk').encode('utf-8')
print(s1)

r+ read and write mode

这里写代码片

Markdown and extensions

Markdown is a lightweight markup language that allows people to write documents in a plain text format that is easy to read and write, which is then converted into richly formatted HTML pages. - [Wikipedia]

Use simple symbols to identify different headings, mark certain text as bold or italic , create a link , etc. See help for detailed syntax? .

This editor supports Markdown Extra , extending many useful functions. Please refer to Github for details .

sheet

Markdown Extra table syntax:

project price
Computer $1600
Phone $12
Pipe $1

Alignment can be defined using colons:

project price quantity
Computer 1600 yuan 5
Phone $12 12
Pipe 1 Yuan 234

Definition list

Markdown Extra  defines list syntax:
item 1
item 2
Definition A
Definition B
item 3
Definition C

Definition D

define D content

code block

Code block syntax follows standard markdown code, for example:

@requires_authorization
def somefunc(param1='', param2=0):
    '''A docstring'''
    if param1 > param2: # interesting
        print 'Greater'
    return (param2 - param1 + 1) or None
class SomeClass:
    pass
>>> message = '''interpreter
... prompt'''

footnote

Generate a footnote 1 .

content

Use [TOC]to generate the directory:

Mathematical formula

Render LaTex math equations with MathJax, see math.stackexchange.com for details .

  • Inline formula, the mathematical formula is: Γ ( n ) = ( n 1 ) ! n N
  • Block level formula:

x = b ± b 2 4 a c 2 a

More LaTex syntax can be found here .

UML php:

Sequence diagrams can be rendered:

Created with Raphaël 2.1.2 张三 张三 李四 李四 嘿,小四儿, 写博客了没? 李四愣了一下,说: 忙得吐血,哪有时间写。

Or flow chart:

Created with Raphaël 2.1.2 开始 我的操作 确认? 结束 yes no
  • For sequence diagram syntax, refer to here ,
  • For flowchart syntax, refer here .

Blogging offline

Even if users do not have a network, they can write blogs offline through this editor (just enter write.blog.csdn.net/mdeditor in the browser they have used before . The Markdown editor uses the browser offline storage to store The content is saved locally.

When a user writes a blog, the content is stored in the browser cache in real time, and the content will not be lost when the user closes the browser or in other abnormal situations. When the user opens the browser again, the unpublished content that the user was editing last time is displayed.

After the blog is published, the local cache will be deleted. 

User can chooseSave the blog you are writing to the server draft, even if you change the browser or clear the cache, the content will not be lost.

Note: Although browser storage is reliable most of the time, for your data security, please be sure to publish it in time or save it to the server draft box after connecting to the Internet .

Browser Compatible

  1. At present, this editor has the most complete support for the Chrome browser. We recommend that you use a newer version of Chrome.
  2. IE9 and below are not supported
  3. IE9, 10, 11 have the following problems
    1. Offline functionality is not supported
    2. IE9 does not support file import and export
    3. IE10 does not support drag and drop file import


  1. Here are the footnotes .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324523613&siteId=291194637