Python reads txt file application --- reads a txt file with python, and writes specified data in each line in the txt file according to the corresponding judgment conditions.

Functional description

Use python to read a txt file, and write the specified data in each line in the txt file according to the corresponding judgment conditions.

f = open("E:/txt/1.txt","r") #添加待读入txt文件路径
line = f.readline()
line = line[:-1]
list = []
while line:             #直到读取完文件
    line = f.readline()  #读取一行文件,包括换行符
    list.append(line)   #八度出来的每一行数据存到list中
f.close() #关闭文件
for i in range(len(list)-1):
    list[i] = list[i][:-1]
    if ((i==249) | (298 <= i <= 415)):
         list[i] = "{},{}".format(list[i], '1\n')
    else:
        list[i] = "{},{}".format(list[i], '0\n')
with open("E:/txt/01.txt","w") as fw: #空文档用来存处理后的数据
    for i in range(len(list)):
        fw.writelines(list[i])
fw.close()

Guess you like

Origin blog.csdn.net/weixin_41022048/article/details/110942983