Python文件操作

pck解包工具 最快捷方便pck解包工具 立即下载

1.文件的读写

1.1.文件的创建与写入

  • 利用内置函数open获取文件对象

  • 文件操作的模式之写入

  • 文件对象的操作方法之写入保存

<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> # coding:utf<span style="color:#880000">-8</span>
 <span style="color:#880000">2</span> 
 <span style="color:#880000">3</span> import os
 <span style="color:#880000">4</span> 
 <span style="color:#880000">5</span> current_path=os.getcwd()
 <span style="color:#880000">6</span> file_path=os.path.<span style="color:#333333"><strong>join</strong></span>(current_path,<span style="color:#880000">'test_os.txt'</span>)
 <span style="color:#880000">7</span> 
 <span style="color:#880000">8</span> f=<span style="color:#333333"><strong>open</strong></span>(file_path,<span style="color:#880000">'w'</span>,<span style="color:#333333"><strong>encoding</strong></span>=<span style="color:#880000">"utf-8"</span>)  #Windows系统在写入中文时需要设置编码格式
 <span style="color:#880000">9</span> f.write(<span style="color:#880000">"Hello Python;你好 Python"</span>)
<span style="color:#880000">10</span> #f.<span style="color:#333333"><strong>read</strong></span>() io.UnsupportedOperation: not readable,控制台报错,因为f是w模式
<span style="color:#880000">11</span> f.<span style="color:#333333"><strong>close</strong></span>()
<span style="color:#880000">12</span> 
<span style="color:#880000">13</span> f=<span style="color:#333333"><strong>open</strong></span>(file_path,<span style="color:#880000">'w+'</span>,<span style="color:#333333"><strong>encoding</strong></span>=<span style="color:#880000">"utf-8"</span>)  #Windows系统在写入中文时需要设置编码格式
<span style="color:#880000">14</span> f.write(<span style="color:#880000">"Hello World;你好 世界"</span>)
<span style="color:#880000">15</span> print(f.<span style="color:#333333"><strong>read</strong></span>()) #无内容输出,<span style="color:#333333"><strong>read</strong></span>读取文件的话  是从最后一个角标读取
<span style="color:#880000">16</span> f.<span style="color:#333333"><strong>seek</strong></span>(<span style="color:#880000">0</span>)       #此时调用<span style="color:#333333"><strong>seek</strong></span>,选择从索引开始读取,则会读取所有刚才写入的字符串
<span style="color:#880000">17</span> print(f.<span style="color:#333333"><strong>read</strong></span>()) #Hello World;你好 世界
<span style="color:#880000">18</span> f.<span style="color:#333333"><strong>close</strong></span>()
<span style="color:#880000">19</span> 
<span style="color:#880000">20</span> f=<span style="color:#333333"><strong>open</strong></span>(file_path,<span style="color:#880000">'ab'</span>)#a:追加,b:比特类型
<span style="color:#880000">21</span> message=<span style="color:#880000">"python很有意思"</span>
<span style="color:#880000">22</span> #message=b<span style="color:#880000">"python很有意思"</span>   SyntaxError: bytes can only contain ASCII literal characters.之前的知识,我们使用b对字符串内容转译成比特类型报错,因为b特类型不能有汉字,所以得采用其他办法
<span style="color:#880000">23</span> _message=message.encode(<span style="color:#880000">"utf-8"</span>)    #将字符串变成比特类型
<span style="color:#880000">24</span> f.write(_message)
<span style="color:#880000">25</span> f.<span style="color:#333333"><strong>close</strong></span>()
<span style="color:#880000">26</span> 
<span style="color:#880000">27</span> f=<span style="color:#333333"><strong>open</strong></span>(file_path,<span style="color:#880000">'a'</span>)
<span style="color:#880000">28</span> message=(<span style="color:#880000">"1\n"</span>,<span style="color:#880000">"2\n"</span>,<span style="color:#880000">"3\n"</span>)
<span style="color:#880000">29</span> f.writelines(message)
<span style="color:#880000">30</span> f.<span style="color:#333333"><strong>close</strong></span>()
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> <span style="color:#888888">#练习1:自动创建python包</span>
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>creat_package</strong></span>(path):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>if</strong></span> os.path.exists(path):
 <span style="color:#880000">7</span>         <span style="color:#333333"><strong>raise</strong></span> Exception(<span style="color:#880000">'%s 已经存在,不可创建'</span>% path)
 <span style="color:#880000">8</span>     os.makedirs(path)
 <span style="color:#880000">9</span>     init_path=os.path.join(path,<span style="color:#880000">'__init__.py'</span>)
<span style="color:#880000">10</span>     f=open(init_path,<span style="color:#880000">'w'</span>)
<span style="color:#880000">11</span>     f.write(<span style="color:#880000">'# coding:utf-8\n'</span>)
<span style="color:#880000">12</span>     f.close()
<span style="color:#880000">13</span> 
<span style="color:#880000">14</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">15</span>     current_path=os.getcwd()
<span style="color:#880000">16</span>     path=os.path.join(current_path,<span style="color:#880000">'test1'</span>)
<span style="color:#880000">17</span>     creat_package(path)
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> <span style="color:#888888">#练习2:写入文件</span>
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Open</strong></span>(object):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>__init__</strong></span>(self,path,mode=<span style="color:#880000">'w'</span>,is_return=True):
 <span style="color:#880000">7</span>         self.path=path
 <span style="color:#880000">8</span>         self.mode=mode
 <span style="color:#880000">9</span>         self.is_return=is_return
<span style="color:#880000">10</span> 
<span style="color:#880000">11</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>write</strong></span>(self,message):
<span style="color:#880000">12</span>         f=open(self.path,mode=self.mode)
<span style="color:#880000">13</span>         <span style="color:#333333"><strong>if</strong></span> self.is_return:
<span style="color:#880000">14</span>             message= <span style="color:#880000">'%s\n'</span> %message
<span style="color:#880000">15</span>         f.write(message)
<span style="color:#880000">16</span>         f.close()
<span style="color:#880000">17</span> 
<span style="color:#880000">18</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">19</span>     current_path=os.getcwd()
<span style="color:#880000">20</span>     open_path=os.path.join(current_path,<span style="color:#880000">'a.txt'</span>)
<span style="color:#880000">21</span>     o=Open(open_path)
<span style="color:#880000">22</span>     o.write(<span style="color:#880000">"Hello World"</span>)
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> <span style="color:#888888">#练习2:容错处理完善</span>
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Open</strong></span>(object):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>__init__</strong></span>(self,path,mode=<span style="color:#880000">'w'</span>,is_return=True):
 <span style="color:#880000">7</span>         self.path=path
 <span style="color:#880000">8</span>         self.mode=mode
 <span style="color:#880000">9</span>         self.is_return=is_return
<span style="color:#880000">10</span> 
<span style="color:#880000">11</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>write</strong></span>(self,message):
<span style="color:#880000">12</span>         f=open(self.path,mode=self.mode)
<span style="color:#880000">13</span>         <span style="color:#333333"><strong>if</strong></span> message.endswith(<span style="color:#880000">"\n"</span>):
<span style="color:#880000">14</span>             self.is_return=<span style="color:#333333"><strong>False</strong></span>
<span style="color:#880000">15</span>             <span style="color:#333333"><strong>if</strong></span> self.is_return:
<span style="color:#880000">16</span>                 message= <span style="color:#880000">'%s\n'</span> %message
<span style="color:#880000">17</span>         <span style="color:#333333"><strong>try</strong></span>:
<span style="color:#880000">18</span>             f.write(message)
<span style="color:#880000">19</span>         <span style="color:#333333"><strong>finally</strong></span>:
<span style="color:#880000">20</span>             f.close()
<span style="color:#880000">21</span> 
<span style="color:#880000">22</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">23</span>     current_path=os.getcwd()
<span style="color:#880000">24</span>     open_path=os.path.join(current_path,<span style="color:#880000">'a.txt'</span>)
<span style="color:#880000">25</span>     o=Open(open_path,<span style="color:#880000">'a'</span>)
<span style="color:#880000">26</span>     o.write(<span style="color:#880000">"Hello World\n"</span>)
</span></span></span></span>

1.2.文件的读操作

  • 文件操作的模式之读

r:返回数据类型为字符串;rb:读取位类型

  • 文件对象的操作方法之读取

<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> 
 <span style="color:#880000">3</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">4</span> 
 <span style="color:#880000">5</span> file_path=os.getcwd()
 <span style="color:#880000">6</span> file=os.path.join(file_path,<span style="color:#880000">'a.txt'</span>)
 <span style="color:#880000">7</span> 
 <span style="color:#880000">8</span> <span style="color:#888888">#read用法,读取文件所有内容,返回的是字符串,read可以和seek配合使用,重置角标去取</span>
 <span style="color:#880000">9</span> f=open(file,<span style="color:#880000">'r'</span>)
<span style="color:#880000">10</span> <span style="color:#888888"># data=f.read()</span>
<span style="color:#880000">11</span> <span style="color:#888888"># print(data)</span>
<span style="color:#880000">12</span> <span style="color:#880000">'''
13 Hello World
14 Hello World
15 Hello World
16 '''</span>
<span style="color:#880000">17</span> 
<span style="color:#880000">18</span> <span style="color:#888888">#readlines用法,返回的是一个列表,每个元素带\n换行</span>
<span style="color:#880000">19</span> <span style="color:#880000">'''
20 data=f.readlines()
21 print(data)     #['Hello World\n', 'Hello World\n', 'Hello World\n']
22 _data=[]
23 for i in data:
24     temp=i.strip()  #strip默认去除空格,换行符等
25     if temp != '':  #去除空行
26         _data.append(temp)
27 print(_data)    #['Hello World', 'Hello World', 'Hello World']
28 '''</span>
<span style="color:#880000">29</span> 
<span style="color:#880000">30</span> <span style="color:#888888">#readline用法,逐行去取</span>
<span style="color:#880000">31</span> <span style="color:#880000">'''
32 data=f.readline()
33 print(data)     #Hello World
34 data=f.readline()
35 print(data)     #123
36 '''</span>
<span style="color:#880000">37</span> 
<span style="color:#880000">38</span> <span style="color:#888888">#mode用法,使用什么方式打开open(path,mode)文件,返回mode的值</span>
<span style="color:#880000">39</span> print(f.mode)   <span style="color:#888888">#r</span>
<span style="color:#880000">40</span> 
<span style="color:#880000">41</span> <span style="color:#888888">#name用法,返回文件名称</span>
<span style="color:#880000">42</span> print(f.name)                       <span style="color:#888888">#D:\WorkSpace\Python_Study\a.txt</span>
<span style="color:#880000">43</span> print(os.path.split(f.name)[<span style="color:#880000">1</span>])     <span style="color:#888888">#a.txt</span>
<span style="color:#880000">44</span> 
<span style="color:#880000">45</span> <span style="color:#888888">#closed用法,文件是否关闭</span>
<span style="color:#880000">46</span> print(f.closed)     <span style="color:#888888">#False</span>
<span style="color:#880000">47</span> f.close()
<span style="color:#880000">48</span> print(f.closed)     <span style="color:#888888">#True</span>
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> os
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> <span style="color:#888888">#文件读写简单封装:</span>
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Open</strong></span>(object):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>__init__</strong></span>(self,path,mode=<span style="color:#880000">'w'</span>,is_return=True):
 <span style="color:#880000">7</span>         self.path=path
 <span style="color:#880000">8</span>         self.mode=mode
 <span style="color:#880000">9</span>         self.is_return=is_return
<span style="color:#880000">10</span> 
<span style="color:#880000">11</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>write</strong></span>(self,message):
<span style="color:#880000">12</span>         f=open(self.path,mode=self.mode)
<span style="color:#880000">13</span>         <span style="color:#333333"><strong>if</strong></span> message.endswith(<span style="color:#880000">"\n"</span>):
<span style="color:#880000">14</span>             self.is_return=<span style="color:#333333"><strong>False</strong></span>
<span style="color:#880000">15</span>             <span style="color:#333333"><strong>if</strong></span> self.is_return:
<span style="color:#880000">16</span>                 message=<span style="color:#880000">'%s\n'</span>%message
<span style="color:#880000">17</span>         <span style="color:#333333"><strong>try</strong></span>:
<span style="color:#880000">18</span>             f.write(message)
<span style="color:#880000">19</span>         <span style="color:#333333"><strong>finally</strong></span>:
<span style="color:#880000">20</span>             f.close()
<span style="color:#880000">21</span> 
<span style="color:#880000">22</span>     <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>read</strong></span>(self,is_strip=True):
<span style="color:#880000">23</span>         result=[]
<span style="color:#880000">24</span>         <span style="color:#333333"><strong>with</strong></span> open(self.path,mode=self.mode) <span style="color:#333333"><strong>as</strong></span> f:
<span style="color:#880000">25</span>             data=f.readlines()
<span style="color:#880000">26</span>         <span style="color:#333333"><strong>for</strong></span> line <span style="color:#333333"><strong>in</strong></span> data:
<span style="color:#880000">27</span>             <span style="color:#333333"><strong>if</strong></span> is_strip:   <span style="color:#888888">#is_strip需不需要去掉每个元素的\n</span>
<span style="color:#880000">28</span>                 temp=line.strip()
<span style="color:#880000">29</span>                 <span style="color:#333333"><strong>if</strong></span> temp != <span style="color:#880000">''</span>:
<span style="color:#880000">30</span>                     result.append(temp)
<span style="color:#880000">31</span>             <span style="color:#333333"><strong>else</strong></span>:
<span style="color:#880000">32</span>                 <span style="color:#333333"><strong>if</strong></span> line !=<span style="color:#880000">''</span>:
<span style="color:#880000">33</span>                     result.append(line)
<span style="color:#880000">34</span>         <span style="color:#333333"><strong>return</strong></span> result
<span style="color:#880000">35</span> 
<span style="color:#880000">36</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">37</span>     carrent_path=os.getcwd()
<span style="color:#880000">38</span>     file_path=os.path.join(carrent_path,<span style="color:#880000">'a.txt'</span>)
<span style="color:#880000">39</span>     o=Open(file_path,mode=<span style="color:#880000">'r'</span>)
<span style="color:#880000">40</span>     data=o.read()
<span style="color:#880000">41</span>     print(data)
</span></span></span></span>

2.文件的应用

2.1.序列化

  • 初识序列化与反序列化
    • 序列化:将对象信息或数据结构信息进行转换,达到存储和传输的目的,最终的数据类型是有一定规则的字符串,可用于文件存储或网络传输
    • 反序列化:通过序列化生成的字符串反转回原来的形式
  • 可序列化的数据类型
    • 可序列化:number,str,list,tuple,dict(最常用的序列化数据类型)
    • 不可序列化:类,实例化对象,函数,集合不能进行序列化
  • Python中的json:通用序列化模块,所有编程语言都有,序列化与反序列化规则统一

  • Python中的pyckle:只使用python编程语言,pickle性能比json好

<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> # coding:utf-<span style="color:#880000">8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> json
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> a=<span style="color:#880000">99</span>
 <span style="color:#880000">5</span> b=<span style="color:#880000">'str'</span>
 <span style="color:#880000">6</span> c=(<span style="color:#880000">1</span>,<span style="color:#880000">2</span>,<span style="color:#880000">3</span>)
 <span style="color:#880000">7</span> d=[<span style="color:#880000">4</span>,<span style="color:#880000">5</span>,<span style="color:#880000">6</span>]
 <span style="color:#880000">8</span> e={<span style="color:#880000">"name"</span>:<span style="color:#880000">"中国"</span>}
 <span style="color:#880000">9</span> 
<span style="color:#880000">10</span> #json序列化
<span style="color:#880000">11</span> a_json=json.dumps(a)
<span style="color:#880000">12</span> <span style="color:#333333"><strong>print</strong></span>(a_json,type(a_json))   #<span style="color:#880000">99</span> <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">13</span> b_json=json.dumps(b)
<span style="color:#880000">14</span> <span style="color:#333333"><strong>print</strong></span>(b_json,type(b_json))   #<span style="color:#880000">"str"</span> <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">15</span> c_json=json.dumps(c)
<span style="color:#880000">16</span> <span style="color:#333333"><strong>print</strong></span>(c_json,type(c_json))   #[<span style="color:#880000">1</span>, <span style="color:#880000">2</span>, <span style="color:#880000">3</span>] <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">17</span> d_json=json.dumps(d)
<span style="color:#880000">18</span> <span style="color:#333333"><strong>print</strong></span>(d_json,type(d_json))   #[<span style="color:#880000">4</span>, <span style="color:#880000">5</span>, <span style="color:#880000">6</span>] <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">19</span> e_json=json.dumps(e)
<span style="color:#880000">20</span> <span style="color:#333333"><strong>print</strong></span>(e_json,type(e_json))   #{<span style="color:#880000">"name"</span>: <span style="color:#880000">"\u4e2d\u56fd"</span>} <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">21</span> 
<span style="color:#880000">22</span> #json反序列化
<span style="color:#880000">23</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(a_json),type(json.loads(a_json)))      #<span style="color:#880000">99</span> <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'int'</span>>
<span style="color:#880000">24</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(b_json),type(json.loads(b_json)))      #str <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'str'</span>>
<span style="color:#880000">25</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(c_json),type(json.loads(c_json)))      #元组反序列化后数据类型变成了列表,[<span style="color:#880000">1</span>, <span style="color:#880000">2</span>, <span style="color:#880000">3</span>] <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'list'</span>>
<span style="color:#880000">26</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(d_json),type(json.loads(d_json)))      #[<span style="color:#880000">4</span>, <span style="color:#880000">5</span>, <span style="color:#880000">6</span>] <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'list'</span>>
<span style="color:#880000">27</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(e_json),type(json.loads(e_json)))      #{<span style="color:#880000">'name'</span>: <span style="color:#880000">'中国'</span>} <<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000">'dict'</span>>
<span style="color:#880000">28</span> 
<span style="color:#880000">29</span> #特殊数据类型进行json序列化与反序列化
<span style="color:#880000">30</span> #布尔类型
<span style="color:#880000">31</span> <span style="color:#333333"><strong>print</strong></span>(json.dumps(<span style="color:#333333"><strong>True</strong></span>))                     #<span style="color:#333333"><strong>true</strong></span>
<span style="color:#880000">32</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(json.dumps(<span style="color:#333333"><strong>True</strong></span>)))         #<span style="color:#333333"><strong>True</strong></span>
<span style="color:#880000">33</span> <span style="color:#333333"><strong>print</strong></span>(json.dumps(<span style="color:#333333"><strong>False</strong></span>))                    #<span style="color:#333333"><strong>false</strong></span>
<span style="color:#880000">34</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(json.dumps(<span style="color:#333333"><strong>False</strong></span>)))        #<span style="color:#333333"><strong>False</strong></span>
<span style="color:#880000">35</span> #空类型
<span style="color:#880000">36</span> <span style="color:#333333"><strong>print</strong></span>(json.dumps(None))                     #<span style="color:#333333"><strong>null</strong></span>
<span style="color:#880000">37</span> <span style="color:#333333"><strong>print</strong></span>(json.loads(json.dumps(None)))         #None
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> # coding:utf<span style="color:#880000">-8</span>
 <span style="color:#880000">2</span> <span style="color:#333333"><strong>import</strong></span> pickle
 <span style="color:#880000">3</span> 
 <span style="color:#880000">4</span> #pickle用法同json,此处不再过多演示
 <span style="color:#880000">5</span> e={<span style="color:#880000">"name"</span>:<span style="color:#880000">"中国"</span>}
 <span style="color:#880000">6</span> 
 <span style="color:#880000">7</span> #pickle序列化
 <span style="color:#880000">8</span> e_pickle=pickle.dumps(e)
 <span style="color:#880000">9</span> <span style="color:#397300">print</span>(e_pickle,<span style="color:#333333"><strong>type</strong></span>(e_pickle))  #b<span style="color:#880000">'\x80\x03}q\x00X\x04\x00\x00\x00nameq\x01X\x06\x00\x00\x00\xe4\xb8\xad\xe5\x9b\xbdq\x02s.'</span> <class <span style="color:#880000">'bytes'</span>>
<span style="color:#880000">10</span> 
<span style="color:#880000">11</span> #pickle反序列化
<span style="color:#880000">12</span> <span style="color:#397300">print</span>(pickle.loads(e_pickle),<span style="color:#333333"><strong>type</strong></span>(pickle.loads(e_pickle)))  #{<span style="color:#880000">'name'</span>: <span style="color:#880000">'中国'</span>} <class <span style="color:#880000">'dict'</span>>
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> 
 <span style="color:#880000">3</span> <span style="color:#333333"><strong>import</strong></span> json
 <span style="color:#880000">4</span> 
 <span style="color:#880000">5</span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>read</strong></span>(path):
 <span style="color:#880000">6</span>     <span style="color:#333333"><strong>with</strong></span> open(<span style="color:#880000">'text.json'</span>,<span style="color:#880000">'r'</span>) <span style="color:#333333"><strong>as</strong></span> f:
 <span style="color:#880000">7</span>         data=f.read()
 <span style="color:#880000">8</span>     <span style="color:#333333"><strong>return</strong></span> json.loads(data)
 <span style="color:#880000">9</span> 
<span style="color:#880000">10</span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>write</strong></span>(path,data):
<span style="color:#880000">11</span>     <span style="color:#333333"><strong>with</strong></span> open(path,<span style="color:#880000">'w'</span>) <span style="color:#333333"><strong>as</strong></span> f:
<span style="color:#880000">12</span>         <span style="color:#333333"><strong>if</strong></span> isinstance(data,dict):
<span style="color:#880000">13</span>             _data=json.dumps(data)
<span style="color:#880000">14</span>             f.write(_data)
<span style="color:#880000">15</span>         <span style="color:#333333"><strong>else</strong></span>:
<span style="color:#880000">16</span>             <span style="color:#333333"><strong>raise</strong></span> TypeError(<span style="color:#880000">'data is dict'</span>)
<span style="color:#880000">17</span>     <span style="color:#333333"><strong>return</strong></span> <span style="color:#333333"><strong>True</strong></span>
<span style="color:#880000">18</span> 
<span style="color:#880000">19</span> data={<span style="color:#880000">"name"</span>:<span style="color:#880000">"张三"</span>,<span style="color:#880000">"age"</span>:<span style="color:#880000">18</span>}
<span style="color:#880000">20</span> 
<span style="color:#880000">21</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">"__main__"</span>:
<span style="color:#880000">22</span>     write(<span style="color:#880000">'text.json'</span>,data)
<span style="color:#880000">23</span>     print(read(<span style="color:#880000">'text.json'</span>))
</span></span></span></span>

2.2.yaml的用法

2.2.1.yaml格式的介绍

2.2.2.Python的第三方包---pyyaml

2.2.3.读取yaml文件的方法

<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> #yaml文件内容
 <span style="color:#880000">2</span> ur<span style="color:#bc6060">l:</span>
 <span style="color:#880000">3</span>   http<span style="color:#bc6060">s:</span>//www.baidu.<span style="color:#333333"><strong>com</strong></span>
 <span style="color:#880000">4</span> <span style="color:#397300">type</span><span style="color:#bc6060">s:</span>
 <span style="color:#880000">5</span>   - 网页
 <span style="color:#880000">6</span>   - 百度百科
 <span style="color:#880000">7</span>   - 图片
 <span style="color:#880000">8</span> dict_1:
 <span style="color:#880000">9</span>   name:<span style="color:#880000">12</span>
<span style="color:#880000">10</span>   age:<span style="color:#880000">18</span>
</span></span></span></span>
<span style="color:#333333"><span style="background-color:#fefefe"><span style="color:#444444"><span style="background-color:#f6f6f6"> <span style="color:#880000">1</span> <span style="color:#888888"># coding:utf-8</span>
 <span style="color:#880000">2</span> 
 <span style="color:#880000">3</span> <span style="color:#333333"><strong>import</strong></span> yaml
 <span style="color:#880000">4</span> <span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>read</strong></span>(path):
 <span style="color:#880000">5</span>     <span style="color:#333333"><strong>with</strong></span> open(path,<span style="color:#880000">'r'</span>,encoding=<span style="color:#880000">"utf-8"</span>) <span style="color:#333333"><strong>as</strong></span> f:
 <span style="color:#880000">6</span>         data=f.read()
 <span style="color:#880000">7</span>     result=yaml.load(data,Loader=yaml.FullLoader)
 <span style="color:#880000">8</span>     <span style="color:#333333"><strong>return</strong></span> result
 <span style="color:#880000">9</span> 
<span style="color:#880000">10</span> <span style="color:#333333"><strong>if</strong></span> __name__==<span style="color:#880000">'__main__'</span>:
<span style="color:#880000">11</span>     print(read(<span style="color:#880000">"text.yaml"</span>))
</span></span></span></span>

回顾:

pck文件编辑工具 可用于pck压缩包的查看和编辑,非常方便,非常快捷的一款工具 立即下载

相关推荐

遥感新手别纠结!实测ENVI 5.3、5.6、6.0三个免费版,教你混搭安装避坑

本文为遥感新手提供ENVI 5.3、5.6、6.0三个免费版的实测对比与混搭安装指南,帮助用户构建零成本高效工作流。详细解析各版本功能差异、共存安装技巧及常见问题解决方案,特别适合课程作业、毕业论文分析和遥感入门练习。

weixin_30819085的博客 479

Python Pickle库原理及使用详解

通过本篇博客,我们深入了解了 Python 中的 pickle 库的原理和使用方法。pickle 库是 Python 中用于序列化和反序列化对象的标准库,它能够帮助我们在不同的程序之间传输和保存数据。但是,在使用 pickle 库时要注意安全性问题,并且谨慎处理不同版本和不信任的数据。希望本文能够帮助你更好地理解 pickle 库,并在实际项目中灵活运用它。

繁依Fanyi的博客 1831

腾讯混元3D世界模型2.0实战:从AI生成到Unity/UE引擎集成全指南

生成式AI正在重塑3D内容创作流程,其核心原理是通过大语言模型理解自然语言描述,并驱动多阶段生成流水线自动创建3D资产与场景。这项技术为游戏开发、虚拟仿真和数字孪生领域带来了革命性的效率提升,能够将传统需要数周的美术协作流程压缩至几分钟。在实际应用中,开发者需要掌握本地环境部署、模型推理优化以及生成资产与主流游戏引擎的集成工作流。本文以腾讯开源的混元3D世界模型为例,详细解析如何将AI生成的场景通过插件高效导入Unity和Unreal Engine,并针对光照烘焙、材质调整、性能优化等工程实践问题提供解决方

cnracht8153的博客 1397

PL_SQL之导出PCK文件

保存pck文件

Mark_mwl的博客 2351

[A-21]ARMv8/v9-SMMU系统架构和功能概述

本文简述了SMMU的系统架构和核心功能

基层架构师的分享 1725

Openclaw Onboarding向导全攻略——如何配置你的第一个AI助手

很多新手在成功运行 `openclaw --version` 后,面对黑底白字的终端就陷入了迷茫:“安装好了,然后呢?” 本篇我们就彻底解决这个问题,带你完整走完 `openclaw onboard` 交互式初始化配置向导的每一步,从“安装完成”到“真正能用”,只差这一个向导的距离。 本文为2026年3月最新版,适配OpenClaw v2026.2.x 稳定版,所有步骤均经过实测验证,可直接落地。

夏日清风 1919

Godot资源提取全攻略:从pck文件解析到游戏逆向工程实战指南

Godot解包工具是游戏开发与逆向工程领域的关键利器,本文将系统介绍pck文件提取方法与游戏资源解析技巧。通过Godot Unpacker这款轻量级工具,开发者可轻松处理非加密的.pck格式资源包,实现游戏资源的高效提取与修改。无论是独立游戏开发中的资源复用,还是游戏研究中的内容分析,本指南都将提供从环境配置到高级参数的完整实操方案。 ## 一、功能解析:Godot Unpacker核心能力揭秘...

gitblog_00367的博客 1154

5分钟搞定!用ZLMediaKit+GB28181搭建低成本外网视频监控(含避坑指南)

本文详细介绍了如何利用开源ZLMediaKit流媒体服务器与国标GB28181协议,快速搭建低成本、可外网访问的企业级视频监控系统。文章提供了从服务端部署、设备配置到外网访问优化的完整实战指南,并重点分享了网络配置、性能调优及常见问题的避坑方案,帮助用户实现高清视频流的稳定推送与多格式播放。

bush的博客 334

Godot资源解包神器:3分钟掌握.pck文件提取技巧

你是否曾经遇到过这样的情况:下载了一个精彩的Godot游戏,想要研究它的美术资源或学习脚本实现,却发现所有内容都被打包在神秘的.pck文件中?传统的解压工具完全无法识别这种格式,让你感到束手无策。今天,我要向你介绍一款专门解决这一难题的神器——**godot-unpacker**,一个专为Godot游戏设计的智能资源解包工具。 ## 🎮 什么是.pck文件?为什么需要解包? 在Godot游戏

gitblog_00269的博客 429

SAP ABAP MIGO增强实战:自定义页签与字段的完整实现指南

本文详细介绍了SAP ABAP中MIGO增强的实战指南,包括自定义页签与字段的完整实现步骤。通过BADI增强技术,开发者可以快速满足企业个性化需求,如添加供应商批次号等特殊字段。文章涵盖数据存储结构设计、BADI框架实现、核心方法编写及数据持久化方案,帮助用户高效完成MIGO屏幕增强开发。

study的专栏 774

域用户无法修改密码问题的解决方案

问题描述:加域的电脑无法更改密码,提示“无法更新密码,为新密码提供的值不符合字符域的长度、复杂度或历史要求”。 问题原因:域控制器中相关设置问题,主要如下图所示(如启用复杂性、最短期限和强制密码历史) 解决方案:在域控制器中更改对应的密码策略即可: ① 禁用复杂性要求 ② 最短使用期限改为0天 ③ 强制密码历史改为0 如果出现无法更改的情况,如下图箭头所指的图标所示 代表需要在域控制器的组策...

运维日记 3万+

【姿态估计】PCK(Percentage of Correct Keypoints)指标及代码实现

姿态估计评价指标PCK

乐乐lelele的博客 7254

cocos2dx中DrawNode画线锯齿和非凸多边形填充

记录一下在cocos2dx中使用DrawNode所遇到的2个问题 1.画线锯齿问题 ios版本下,在AppController.mm中设置CCEAGLView的multiSampling和numberOfSamples。如下图 CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]

hello-world 5415

Python中对基本文件操作

Python中对基本文件操作,一起来学习吧(一起努力,咱们顶峰相见)

精益求精,永无止境,永远在路上! 7859

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

python 关于文件操作——基础详细

python 文件操作、文件读写(write、read、readlines、readline)、文件的相关操作,简单易懂 目录1 文件操作1.1 文件打开与关闭1.1.1 打开文件1.1.2 关闭文件1.2 访问模式及说明2 文件读写2.1 写数据(write)2.2 读数据(read)2.3 读数据(readlines)2.3 读数据(readline)3 文件的相关操作3.1 文件重命名3.2 删除文件3.3 创建文件夹3.4 获取当前目录3.5 改变默认目录 1 文件操作 文件就是把一些数据存起来,

ChenBinBini的博客 3万+

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

python文件操作

python对文件的常见操作以及代码演示

。 1843

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Python文件操作详解

一:打开和关闭文件①:open 函数        你必须先用Python内置的open()函数打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写。语法:file object = open(file_name [, access_mode][, buffering])   各个参数的细节如下:      file_name:是一个包含了你要访问的文件名的字符串值。      ac...

JOKER___Z的博客 2万+

Python 文件操作详解

本文全面介绍了Python文件操作的核心知识,包括基本流程、打开模式、常用方法和实用场景。文章详细对比了各种操作模式的差异,并提供了大量代码示例,特别推荐使用with语句自动处理文件关闭,是掌握Python文件处理的实用指南。

x15514104477的博客 1095

python-文件操作

os.scandir() 用于获取指定目录中的条目,并返回一个包含 DirEntry 对象的迭代器。每个 DirEntry 对象提供了文件或目录的基本信息,如名称、路径、文件类型等。与 os.listdir() 相比,os.scandir() 提供了更高效的文件遍历,能够直接访问文件类型信息,而不需要额外的系统调用。3.7 文件拷贝-

qq_27833723的博客 2499

Python文件操作总结

Python文件操作 文件 计算机中数据持久化的表现形式 读写文件标准格式一 打开

阿鹏的博客 1661

Python教程——python文件操作详解

所谓的文件操作是指对计算机中的文件进行读取、写入、修改和删除等操作。就是这三个简简单单的操作,却在计算机世界占有一席之地。打开文件有各种打开模式,各不相同;操作文件,有读写模式;关闭文件就比较简单了。

mate1357的博客 1616

Python之文件操作详解

Python文件操作 Python文件操作示例

weixin_42771366的博客 3370

python中文件操作中有什么困难_浅谈python中的文件操作

对于文件的操作在python中占有相当大的分量,今天我们就从新人的角度来看看python中一些简单的文件操作。python中文件操作一共有六种常用模式,分别是‘r’,‘w’,‘a’,‘r+’,‘w+’,‘a+’。首先,我们先创建一个文件‘file2’,并将如下内容写进文件:一张褪色的照片,好像带给我一点点怀念。巷尾老爷爷卖的热汤面,味道弥漫过旧旧的后院;流浪猫睡熟在摇晃秋千,夕阳照了一遍他咪着眼;...

weixin_39885383的博客 134
上一篇: 如何用Python保存语音、图片、视频等信息转发给好友
下一篇: Python pyecharts绘制词云图
Python_xiaowu
博客等级 码龄5年 3096粉丝 178原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值