#文本及二进制形式打开文件
tf=open('t.txt','rt',encoding = 'utf-8')
print(tf.readline())
tf.close
tf=open('t.txt','rb')
print(tf.readline())
tf.close
b''
r
:只读模式,默认值,如果文件不存在,返回 FileNotFoundErrorw
:覆盖写模式,文件不存在则创建,存在则完全覆盖x
:创建写模式,文件不存在则创建,存在则返回 File Exists Errora
:追加写模式,文件不存在则创建,存在则在文件最后追加内容b
:二进制文件模式t
:文本文件模式,默认值+
:与r/w/x/a同使用,在原功能基础上增加同时读写功能.read(size=-1)
:读入全部内容,如果给出参数,就输入size长度.readline(size=-1)
:读入一行内容,读入该行的size长度.readlines(hint=-1)
:读入文件所有行,如给出参数,读入hint行.write(s)
:向文件写入一个字符串或字节流.writelines(lines)
:将一个元素全为字符串的列表写入交件.seek(offset)
:改变当前文件操作指针位置的操作,0-文件开头;1-当前位置;2-文件结尾#AutoTraceDraw
import turtle as t
t.title('自动绘制轨迹')
t.setup(800,600,0,0)
t.pencolor('red')
t.pensize(5)
#数据类型
datals=[]
f=open('data.txt')
for line in f:
line = line.replace('\n','')
datals.append(list(map(eval,line.split(',')))) #map函数可以将第一个函数的功能作用于第二个参数
f.close
#自动绘制
for i in range(len(datals)):
t.pencolor(datals[i][3],datals[i][4],datals[i][5])
t.fd(datals[i][0])
if datals[i][1]:
t.right(datals[i][2])
else:
t.left(datals[i][2])
#读入
txt = open('t.txt',encoding='utf-8')
tf=txt.read()
ls = tf.split()
print(ls)
txt.close
#写入
f = open('test.txt','w',encoding='utf-8')
f.write(' '.join(ls))
f.close()
print(ls)
[]
[]
#从csv文件中读入数据
fo = open(fname)
ls=[]
for line in fo:
line = line.replace('\n','')
ls.append(line.split(','))
fo.close
#将数据写入csv数据
ls=[[],[],[]]#二维列表
f=open(fname,'w')
for item in ls:
f.write(','.join(item)+'\n')
f.close
#二维数据的逐一处理
ls=[[1,2],[3,4],[5,6]]
for row in ls:
for column in row:
print(column)
#wordcloud库常规方法
import wordcloud
import matplotlib as plt
%matplotlib inline
c=wordcloud.WordCloud(width=500,height=200)
c.generate('wordcloud by python')
c.to_file('pywordcloud.png')
import wordcloud
txt='life is short, you need python'
w=wordcloud.WordCloud(background_color='white')
w.generate(txt)
w.to_file('pywcloud.png')
#GovRptWordCLoudv1
import jieba
import wordcloud
f= open('小康.txt','r',encoding='utf-8')
t=f.read()
f.close
ls=jieba.lcut(t)
txt=' '.join(ls)
w=wordcloud.WordCloud(font_path='C:\\$Recycle.Bin\\S-1-5-21-956881968-3683699883-3077907830-500\\$R3UWT13\\Updates\\Download\\PackageFiles\\F48252D1-E2D1-4E3D-A011-C1A468BFE8F4\\root\\vfs\\Fonts\\private\\MSYH.TTC',width=1000,height=700,background_color='black',max_words=15)
w.generate(txt)
w.to_file('建成小康.png')
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\ADMINI~1\AppData\Local\Temp\jieba.cache
Loading model cost 0.598 seconds.
Prefix dict has been built successfully.
#GovRptWordCLoudv2
import jieba
import wordcloud
import imageio
mask=imageio.imread('中国地图.jpeg')
f= open('小康.txt','r',encoding='utf-8')
t=f.read()
f.close
ls=jieba.lcut(t)
txt=' '.join(ls)
w=wordcloud.WordCloud(font_path='C:\\$Recycle.Bin\\S-1-5-21-956881968-3683699883-3077907830-500\\$R3UWT13\\Updates\\Download\\PackageFiles\\F48252D1-E2D1-4E3D-A011-C1A468BFE8F4\\root\\vfs\\Fonts\\private\\MSYH.TTC',width=1000,height=700,background_color='white',max_words=150,mask=mask)
w.generate(txt)
w.to_file('建成小康.png')
打印输出附件文件的平均列数,计算方法如下:
(1)有效行指包含至少一个字符的行,不计算空行;
(2)每行的列数为其有效字符数;
(3)平均列数为有效行的列数平均值,采用四舍五入方式取整数进位。
with open('latex.log', 'r', encoding='utf-8') as f:
row_cnt=0
char_cnt=[]
for line in f:
line = line.strip('\n')
if line == '':
continue
char_cnt.append(len(line))
row_cnt+=1
f.close()
print(round(sum(char_cnt)/row_cnt))
48
描述
附件是一个CSV格式文件,提取数据进行如下格式转换:
(1)按行进行倒序排列;
(2)每行数据倒序排列;
(3)使用分号(;)代替逗号(,)分割数据,无空格;
with open('data.csv', 'r', encoding='utf-8') as f:
lines=f.readlines()
lines.reverse()
for line in lines:
line = line.replace('\n','')
line=line.replace(' ','')
t=line.split(',')
t.reverse()
print(';'.join(t))
3;8;6;1;7;4;2;5
'k';'j';'i';'c';'z';'x';'b';'y';'a'
'x';'y';'j';'i';'k';'a';'b';'c';'z'
'x';'a';'z';'y';'i';'c';'j';'b';'k'
'k';'j';'i';'z';'y';'x';'c';'b';'a'
2;4;7;5;8;3;1;6
5;6;4;1;7;2;3;8
7;6;5;4;3;2;1