使用split函数的时候注意将list转为str
file1 = open(r'C:\Users\wengjiaqi\Desktop\test1.txt','r',encoding='utf-8')
filecontent = file1.readlines()
data=','.join(filecontent)
data1=data.split("#")
for j in data1:
print(j)
a=['c','a','t']
b=''
print(b.join(a))
c='-'
print(c.join(a))
Python中读写文件
file1 = open(r’C:\Users\wengjiaqi\Desktop\test1.txt’,‘r’,encoding=‘GBK’)
filecontent = file1.readlines()
file1.close()
listvar = []
for i in filecontent:
data = i.strip().split(’,’)
sum = 0
for score in data[1:]:
sum = sum + int(score)
result=data[0]+str(sum)
print(result)
writer = open(r’C:\Users\wengjiaqi\Desktop\test1.txt’,‘a’,encoding=‘GBK’)
writer.write(’\n’)
writer.writelines(listvar)
writer.close()