Python(26)同时对两个文件读写

with open(inFileName) as file1, open(outFileName,'w') as file2:
    file_line= file1.read().splitlines()
    # 或者可用  file_line= file1.read().split('\n')
    for one in file_line:
        if one.count(';') != 1:
            continue
        name,pwd= one.split(';') 
        file2.write(name+ '\n')

实战

# 第一种写法

with open('./log.csv', encoding='gbk') as rf, open('./token','a+') as wf:
        reader = csv.reader(rf)
        for val in reader:
            token = re.findall('token":"(.*?)"}', val[0])[0]
            wf.write(token+'\n')
# 第2种写法

with open('./log.csv', encoding='gbk') as rf, open('./token','a+', newline='') as wf:
        reader = csv.reader(rf)
        writer = csv.writer(wf)
        for val in reader:
            token = re.findall('token":"(.*?)"}', val[0])
            writer.writerow(token)

你可能感兴趣的:(python)