当前位置: 首页 > news >正文

南通网络公司网站搜索推广广告

南通网络公司网站,搜索推广广告,开发公司如何加强财务管理,做漫画的网站有哪些文件的写入 和文件的读取一样,文件的写入也有多种方法,write()和writelines()方法。 二者之间的区别是: write()方法用于将字符串写入文件,如果要写入文件的字…

文件的写入
和文件的读取一样,文件的写入也有多种方法,write()和writelines()方法。
二者之间的区别是:
write()方法用于将字符串写入文件,如果要写入文件的字符串不多,使用write()方法即可

writelines()用于将列表中存储的字符串写入到文件中,用于将大量的字符串写入到文件中,以提高效率。
例如:
myfile = ("hello.txt", "aw+")
temp = ["hello world!\n"]
myfile.writelines(temp)    #writelines()的使用

strin ="hello!"
myfile.write(strin)              #write()的使用
myfile.close()

文件的删除
文件的删除需要使用到os模块和os.path模块。os提供了对系统环境,文件,目录等操作系统级的接口函数。
文件的删除使用remove()函数。
演示如下:
myfile = ("myfile.txt", "w+")
if os.path.exists("myfile.txt"):     #判断文件是否存在,注意后面的冒号
os.remove("myfile.txt")       



文件的复制
file类中没有提供专门的文件复制函数,因此只能通过使用文件的读写函数来实现文件的复制。这里仅仅给出范例:
src = file("myfile.txt", "w+")
temp = ["hello world! \n"]
src.writelines(temp)
src.close()

src = file("myfile.txt", "r+")
des = file("myfile2.txt", "w+")
des.writelines(src.read())
src.close()
des.close()

shutil模块是另一个文件,目录的管理接口,提供了一些用于复制文件,目录的函数。

copyfile()函数可以实现文件的拷贝,声明如下:
copyfile(src, des)
文件的剪切可以使用move()函数模拟,声明如下:
move(src,des)
功能:移动一个文件或者目录到指定的位置,并且可以根据参数des重命名移动后的文件。

使用shutil来实现文件的拷贝
import shutil

shutil.copyfile("myfile1.txt", "myfile2.txt")
shutil.move("myfile1.txt", "../")                 #把myfile1.txt移动到当前目录的父目录,然后删除myfile1.txt
shutil.move("myfile2.txt", "myfile3.txt") #把myfile2.txt移动到当前目录并重命名myfile3.txt

os模块的函数rename()可以对文件或者目录进行重命名。


下面演示文件重命名操作。

如果当前目录存在名为myfile.txt的文件,则重命名为myfile_rename.txt.

import os

li = os.listdir(".")                                   #返回当前目录的文件列表

print li                                               #打印出当前目录包含的文件

if myfile.txt in li:

       os.rename("myfile.txt", "myfile_rename.txt")


上面例子中是修改文件名,但是文件还是统一类型, 文件的后缀名没变,有的时候需要将一种类型的文件改成另一种类型的文件这是就得利用rename()和字符串查找的函数。

示例如下:将后缀名为“html”格式的文件改成“htm”格式的文件

import os

files = os.listdir(".")

for filename in files:

      pos = filename.find(".")

      if filename[pos+1:] == "html" :

             newname = filename[:pos+1] +"htm"

             os.rename(filename, newname)

以上过程还可以通过splitext()来实现,splitext()用于将文件名和后缀名分隔开。

import os

files = os.listdir(".")

for  filename in files :

       li = os.path.splitext(filename)      #返回文件名和后缀名组成的列表

       if li[1] == "html":

              newname = li[0] + "htm"

              os.rename(filename, newname)


路径的匹配可以使用glob模块,返回符合给定匹配条件的文件列表。例如上面的例子需要判断文件后缀是否是“html”类型,可以使用glob()直接进行匹配:glob.glob(“*.html”)



文件内容的查找和替换主要通过演示来说明其实现方法
【1】文件内容的查找:从hello.txt中查找字符串“hello”, 并统计“hello”出现的次数。
import re
myfile = file("hello.txt", "r+")
count = 0
for s in myfile.readlines:          #每次从hello.txt中读取一行,保存到s中


 li = re.findall("hello", s)   #调用findall()查询s, 并将查询到的结果保存到li中


      if len[li] > 0:             

#如果列表元素大于0,则表示查询到字符串“hello”
             count =  count + li.count("hello")
print "查找到" + str(count) + "个hello"   #调用count()统计当前列表中“hello“出现的次数
myfile.close()

【2】文件内容的替换:把hello.txt中的hello全部换为”hi“,并把结果保存到myhello.txt中。
f1 = file("hello.txt", "r")

f2 = file("myhello.txt", "w")
for s in f1.readlines():
      f2.writes(s.replace("hello","hi"))            #调用replace函数将s中

的“hello”替换为“hi”,
                                                                         #并把结果写入myhello.txt中
f1.close()
f2.close()




python中的文件操作和C语言里面的文件操作思想相同,都是分为三步,即打开文件,读写文件,最后要关闭文件,只不过使用的函数不一样罢了。下面就稍微详细点的进行说明:
(1)创建并且打开文件,使用file()或者open()函数,如果要打开的文件存在,则打开,否则创建该文件。
(2)调用read(),write()函数对文件进行读写。
(3)和C语言一样调用close()函数进行关闭文件。

举例如下:
# -*- coding: UTF-8 -*-

myfile = file("hello.txt", "w+")
myfile.write("奥巴马, 美国总统")
myfile.close()

myfile = file("hello.txt","r+")
myfile = read()
print myfile
myfile.close()

文件的读函数:
文件的读函数主要分为readline(), readlines(),read()函数。
readline()每次读取文件的一行,因此需要使用永真式来判断是否已经读到文件结尾。
举例如下:
myfile = file("hello.txt", "r")   #打开文件
while true:                             #读文件
         line = myfile.readline()
         if line:
                 print line
         else:
                 break
myfile.close()                         #关闭文件

readlines()的使用和readline()差不多, 只不过readlines()一次性读取多行,并且也许要通过循环返回列表中的元素。
举例如下:
myfile = file("hello.txt", "r+")
lines = myfile.readlines()   # readlines()的返回值为列表。


if line in lines:                       #逐行读取列表的值
       print line
myfile.close()

read()函数是从文件中一次性读取所有内容,并赋给一个字符串变量
举例如下:
myfile = file("hello.txt", "r+")
lines = myfile.read()
print  lines
myfile.close()



在开发过程中通常要对字典进行排序,复制等操作,和列表一样,字典的排序也采用sorted()函数,字典的复制除了可以使用update()函数外,还可以使用copy()函数,但是得注意二者之间的区别。

 

字典的排序

先来演示字典的排序函数sort()的使用:

dict = {"a":"apple","b":"banana","g":"grape","c":"oreage"}

print dict

#按照key进行排序。items可以用于字典的遍历,返回(key,value)的序列,lampda用于创建匿名函数,并返回计算结果,d[0]表示key

print sorted(dict.items(), key=lampda d:d[0])

#按照value进行排序。同样d[1]表示value

print sorted(dict.items(), key=lampda d:d[1])

 

字典的复制

字典的复制前面用的是update函数,这个方法是将字典A中的数据复制到字典B中,且字典中原有的数据保持不变,从而实现了字典B中数据的扩充。但是sorted不同,将字典A中的数据复制到字典B中,update会清除掉字典B中原有的数据。另外copy函数实现的是字典的浅拷贝,deepcopy函数用于实现深拷贝。

下面演示copy函数的使用。

dict ={"a":"apple","o":"orange"}

dict2 = {"g":"grape","b":"banana"}

dict2 = dict.copy()

print dict2

dict2输出:{"a":"apple","o":"orange"}

浅拷贝只是复制数据,数据的引用并没有被复制,因此新的数据和旧的数据使用同一块内存块,

深拷贝则不一样,它拷贝对象内部所有数据和引用,相当于C语言中指针的作用。例如:字典B浅拷贝字典A中的数据,如果字典B中的数据发生修改,字典A中的数据也将发生变化;但是如果字典B深拷贝字典A中的数据,则即使B中的数据变了,A中也不会变。

下面演示深拷贝和浅拷贝:

import copy

dict ={"a":"apple","o":"orange"}

dict2 ={"b":"banana","p":"pear"}

#copy.deepcopy等价于dict.deepcopy

dict2 = copy.deepcopy(dict)

#copy.copy 等价于dict.copy

dict3 = copy.copy(dict)

dict2["a"]="watermelon"

print dict

dict3["a"]="watermelon"

print dict



文章转载自:
http://repurchase.wwxg.cn
http://stunted.wwxg.cn
http://procoagulant.wwxg.cn
http://clonus.wwxg.cn
http://polybasite.wwxg.cn
http://mushily.wwxg.cn
http://rimpled.wwxg.cn
http://checkstring.wwxg.cn
http://millpond.wwxg.cn
http://postcava.wwxg.cn
http://tomnoddy.wwxg.cn
http://mambo.wwxg.cn
http://backstretch.wwxg.cn
http://neutralistic.wwxg.cn
http://neutretto.wwxg.cn
http://sexpartite.wwxg.cn
http://dab.wwxg.cn
http://bioinstrumentation.wwxg.cn
http://nightviewer.wwxg.cn
http://schmagagi.wwxg.cn
http://physician.wwxg.cn
http://shrill.wwxg.cn
http://appassionata.wwxg.cn
http://infusorial.wwxg.cn
http://kamaishi.wwxg.cn
http://paracasein.wwxg.cn
http://aisled.wwxg.cn
http://encyst.wwxg.cn
http://insinuate.wwxg.cn
http://jinriksha.wwxg.cn
http://overground.wwxg.cn
http://galvanic.wwxg.cn
http://trio.wwxg.cn
http://affirmation.wwxg.cn
http://calisaya.wwxg.cn
http://gnaw.wwxg.cn
http://immiscible.wwxg.cn
http://enarch.wwxg.cn
http://adrienne.wwxg.cn
http://hellbox.wwxg.cn
http://hl.wwxg.cn
http://volt.wwxg.cn
http://whsle.wwxg.cn
http://zygophyte.wwxg.cn
http://hempseed.wwxg.cn
http://sphingomyelin.wwxg.cn
http://enisei.wwxg.cn
http://convincing.wwxg.cn
http://affectionateness.wwxg.cn
http://tailstock.wwxg.cn
http://incontinently.wwxg.cn
http://leander.wwxg.cn
http://irgun.wwxg.cn
http://vermian.wwxg.cn
http://untapped.wwxg.cn
http://tripodal.wwxg.cn
http://abscondence.wwxg.cn
http://ichthyofauna.wwxg.cn
http://rfz.wwxg.cn
http://barsac.wwxg.cn
http://milliwatt.wwxg.cn
http://piety.wwxg.cn
http://amitrol.wwxg.cn
http://leu.wwxg.cn
http://plowing.wwxg.cn
http://lickspit.wwxg.cn
http://taws.wwxg.cn
http://cognition.wwxg.cn
http://intragalactic.wwxg.cn
http://epopee.wwxg.cn
http://indorsee.wwxg.cn
http://ankyloglossia.wwxg.cn
http://divulgate.wwxg.cn
http://copter.wwxg.cn
http://lifetime.wwxg.cn
http://correspond.wwxg.cn
http://photoscope.wwxg.cn
http://underbid.wwxg.cn
http://putridity.wwxg.cn
http://nagged.wwxg.cn
http://florescence.wwxg.cn
http://juruena.wwxg.cn
http://direfully.wwxg.cn
http://soundscape.wwxg.cn
http://posttreatment.wwxg.cn
http://crizzle.wwxg.cn
http://eudemonism.wwxg.cn
http://ataractic.wwxg.cn
http://yellowish.wwxg.cn
http://prof.wwxg.cn
http://womanity.wwxg.cn
http://willow.wwxg.cn
http://centrilobular.wwxg.cn
http://nordstrandite.wwxg.cn
http://whippoorwill.wwxg.cn
http://fantastico.wwxg.cn
http://sacaton.wwxg.cn
http://sheristadar.wwxg.cn
http://gilbertian.wwxg.cn
http://heartful.wwxg.cn
http://www.hrbkazy.com/news/75662.html

相关文章:

  • 网站logo代码网络营销的有哪些特点
  • 北京建设网经济适用房广东短视频seo搜索哪家好
  • 做网站需要许可证吗长尾词挖掘
  • 北京工程建设交易中心网站培训机构哪家好
  • 作品集的个人网站怎么做惠州seo排名收费
  • 网站开发合同样本关键时刻
  • 淘宝客做网站备注怎么写的免费引流人脉推广软件
  • 欧产日产国产水蜜桃湖南企业竞价优化
  • 高端网站建设公司联系电话北京百度公司总部电话
  • 杭州做网站公司短视频营销的优势
  • 网站建设有什么意义谷歌play商店官网
  • 安徽省交通运输厅网站知乎关键词排名优化
  • 自己做的网站不能用手机访问营销推广外包公司
  • 做网站没有活品牌营销策略有哪些方法
  • 网站真实性检验单seo建站还有市场吗
  • 杭州做产地证去哪个网站seo模拟点击工具
  • wordpress无法管理站点谷歌搜索引擎免费入口 香港
  • 网站很难被百度收录网站推广方案策划书2000
  • 大连做网站优化哪家好seo推广效果怎么样
  • 做网站软件wd最近中国新闻热点大事件
  • 青岛网络推广建站百度权重高的网站有哪些
  • 郑州专业网站制作建设培训
  • 做壁纸网站的意义百度新闻网页
  • 如何做网页游戏网站网站seo视频
  • 昆明网站测试公司常德网站建设公司
  • 电影网站页面seo站长seo查询
  • 暗红色网站冬镜seo
  • wordpress表白墙天津网站优化软件
  • 济南网站制作设计公司专业优化网站排名
  • 个人养老金制度要来了网络营销中的seo是指