1.read file
infile = open(fileName,'r')
#for line in infile:#遍历文件
listVar = [line.rstrip() for line in infile]将每一行生成列表
strVar = infile.read()#读取文件全部内容置于一个字符串
strVar = infile.readline()#读取当前指针指向的那一行,刚开始是置于文件第一行,执行语句后,将置于末尾。准备读取下一行
infile.close()#close file
Test.txt文件中内容如下
Google
Baidu
Yahoo
其实是以下的方式
Google\nBaidu\nYahoo\n
2.create file
outfile = open(fileName,'w')
outfile.write(strVar)
outfile.writelines(strVar)
#file.write(str)的参数是一个字符串,就是你要写入文件的内容.
#file.writelines(sequence)的参数是序列,比如列表,它会迭代帮你写入文件.
outfile.close()
3.opened for append
outfile = open(fileName,'a')#打开文件在文件末尾添加
outfile.write(strVar)
outfile.writelines(strVar)
outfile.close()
4.set
list列表是元素的顺序存储容器,而且允许元素重复
set集合是元素的无序存储容器,不允许元素重复
{"spam","ni"},{3,4,7},{True,7,"eleven"},{'a','b',(3,4)}#以上都是set
#集合操作
word = {"spam","ni"}
set.add()#word.add("eggs")#words = {"spam","ni","eggs"}
set.discard()#discard丢弃,抛弃; 解雇; 出牌;word.discard("ni")words = {"spam"}
set.clear()#words.clear() words = {}
set#set([3,7,3])#{3,7}
set#set((3,7,3))#{7,3}
{x*x for x in range(-3,3)}#{0,1,4,9}
set1.union(set2)#set1∪set2
set1.intersection(set2)#set1∩set2
set1.difference(set2)#set1-set2
File1.txt
Alpha
Bravo
Charlie
File2.txt
Bravo
Delta
def main():
infile = open("File1.txt",'r')
firstSet = {line.rstrip() for line in infile}
infile.close()
infile = open("File2.txt",'r')
firstSet = {line.rstrip() for line in infile}
infile.close()
outfile = open("Union.txt",'w')
outfile.writelines(set1.union(set2))
outfile.close()
outfile = open("Intersection.txt",'w')
outfile.writelines(set1.intersection(set2))
outfile.close()
outfile = open("Difference.txt",'w')
outfile.writelines(set1.difference(set2))
outfile.close()
main()
Union.txt#Alpha,Bravo,Charlie,Delta
Intersection.txt#Bravo
Difference.txt#Alpha,Charlie
5.csv文件
将数据以逗号分隔(当然也可以不是逗号)
譬如csvDemo.csv
name,passwd,age,address
zhangsan,123456,15,sichuang
lisi,45612,15,jiangshu
可以在excel中打开
UN.txt
Canada,North America,34.8,385500
France,Europe,66.3,211209
New Zealand,Austrlia/Oceania,4.4,103738
Nigeria,Africa,177.2,356669
Pakistan,Asia,196.2,310403
Peru,South America,30.1,496226
#-----------------
def main():
continent = input("Enter the name of a continent:")
continent = continent.title()#Allow for all lowercase letters
if continent != 'Antarctica':
infile = open("UN.txt",'r')
for line in infile:
data = line.split(',')
if data[1] == continent:
print(data[0])
else:
print("There are no counteries in Antarctica")
#-------
Enter the name of a continent:North America
Canada
6.dict字典
bob = {"firstName":"Robert","lastName":"Smith","age":19}
print(bob["firstName"],bob["lastName"],"is",bob["age"],"years old.")
[RUN]
Robert Smith is 19 years old.
dict操作
len(d)#字典中元素(键值对)的个数
x in d#如果x是字典的一个键,返回True
x:y in d#如果x:y是字典中的元素,返回True#not in
d[key1] = value1#返回key1对应的值,否者抛出异常
d.get(key1,default)#如果存在key1则返回value1,否者返回default
list(d.keys())#返回字典键组成的list
list(d.values())#返回字典值组成的list
list(d.items())#返回(key,value)形成的二元组组成的列表,其中d[key]=value
list(d)#set(d)#tuple(d)#返回字典的键组成的列表,集合,元组
c = {}#创建一个字典
c = dict(d)#创建字典d的一个拷贝
d.update(c)#将字典c所有的元素合并入字典d。如果两个元素拥有相同的键,则使用c中的值替换d中的值
实例
eg 1:
def translate(color):#英语翻译成意大利语
if color == "red":
return "rojo"
elif color == "blue":
return "aloz"
elif color == "green":
return "verdi"
elif color == "white":
return "blanco"
#-----
translate = {"red":"rojo","blue":"aloz","green":"verdi","white":"blanco"}
eg 2:
list1 = [["one",1],["two",2],["three",3]]
或list1 = [("one",1),("two",2),("three",3)]
dict(list1)
{"one":1,"two":2,"three":3}
eg 3:
Textese.txt
anyone,nei
are,r
ate,8
band,b&
be,b
#-----
def main():
texteseDict = createDictionary("Textese.txt")
print("Enter a simple sentence in lowercase letters without")
sentence = input("any punctuation: ")
print()
translate(sentence,texteseDict)
def createDictionary(fileName):
infile = open(fileName,'r')
textList = [line.rstrip() for line in infile]
infile.close()
return dict([var.split(',') for var in textList])
def translate(sentence,texteseDict):
words = sentence.split()
for word in words:
print(texteseDict.get(word,word)+" ",end = " ")
main()
耗时2h写好