Index=Index+1
iftext[Index]=='':
text=text[0:i+1]+text[Index+1:len(text)]#字符串拼接
break
file_object=open("6.txt","w",encoding='utf-8')#读取文件改为写模式(蒙语文本utf-8编码)
#file_object=open('6.txt','w')读取文件
file_object.write(text)
file_object.close()
参考https://blog.youkuaiyun.com/qq_27158179/article/details/82883971
这有一点需要注意到的:因为处理的是蒙语文本,被读取和被写入文件的编码格式是使用utf-8,所以需要注意读取文件的操作。
(2)删除每行最前的空格
withopen('6.txt',encoding='utf-8')asfr:#读取文件(蒙语文本utf-8编码)
withopen("8.txt","w",encoding='utf-8')asfw:#读取文件改为写模式(蒙语文本utf-8编码)
forlineinfr.readlines():
fw.write(line.lstrip()+'')#lstrip这个函数是去除左边的空白
(3)给每行最前面添加数字和空格
#在每行之前添加序号
#coding:UTF-8#设置编码
ff=open('3.txt','w')#打开一个文件,可写模式
withopen('2.txt','r')asf:#打开一个文件只读模式
line=f.readlines()#读取文件中的每一行,放入line列表中
forindex,line_listinenumerate(line):
line_new=line_list.replace('','')#将换行符替换为空('')
index=str(index)#强制转化数字为字符串
line_new=index+''+line_new+''#行末尾加上"|",同时加上""换行符
#print(line_new)
ff.write(line_new)#写入一个新文件中
参考https://blog.youkuaiyun.com/jacke121/article/details/78246081
参考https://www.cnblogs.com/ywjfx/p/9998464.html
注意:写入文件的时候不可以用'int‘+‘str’所以需要将数字强制转换为’str’