python循环写入txt文件,防止内容被覆盖

import numpy as np

if __name__ == '__main__':
    guid = 0
    tokens = np.array(['CLS', 'i', 'want', 'to', 'fly', 'from', 'baltimore', 'to', 'dallas', 'round', 'trip', 'SEP'])
    input_ids = [101, 1045, 2215, 2000, 4875, 2013, 6222, 2000, 5759, 2461, 4440, 102, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    attention_mask = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    token_type_ids = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                      0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    intent_label = 12
    slot_labels_ids = [-100, 2, 2, 2, 2, 2, 73, 2, 114, 98, 99, -100, -100, -100, -100, -100, -100, -100, -100, -100,
                       -100,
                       -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100,
                       -100,
                       -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100, -100]

    a = str(guid)
    b = [str(x) for x in tokens]
    c = [str(x) for x in input_ids]
    d = [str(x) for x in attention_mask]
    e = [str(x) for x in token_type_ids]
    g = str(intent_label)
    h = [str(x) for x in slot_labels_ids]
    # 写入文件时不能直接写入list,此时利用join()函数写入
    with open('atis_train.txt', 'a', encoding='utf-8') as f:
        f.write('guid:' + a + '\n')
        f.write('tokens:')
        f.write(''.join([str(x) + ' ' for x in tokens]) + '\n')
        f.write('input_ids:')
        f.write(''.join([str(x) + ' ' for x in input_ids]) + '\n')
        f.write('attention_mask:')
        f.write(''.join([str(x) + ' ' for x in attention_mask]) + '\n')
        f.write('token_type_ids:')
        f.write(''.join([str(x) + ' ' for x in token_type_ids]) + '\n')
        f.write('intent_label:')
        f.write(''+ g + '\n')
        f.write('slot_labels:')
        f.write(''.join([str(x) + ' ' for x in slot_labels_ids]) + '\n')
    f.close()
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值