python---在已有数据的文件中的第一行插入指定数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_34613450/article/details/88657549

定义如下函数即可(以csv文件为例):

"""在csv文件中第一行添加索引字段"""
def write_raw_index(file):
    filename = file
    with open(filename, 'r+', encoding='utf-8') as f:
        content = f.read()
        f.seek(0, 0)
        #mid, text, source, uid
        text = 'mid' + ',' + 'text' + ',' + 'source' + ',' + 'uid'
        f.write(text + '\n' + content)

猜你喜欢

转载自blog.csdn.net/weixin_34613450/article/details/88657549