zoukankan      html  css  js  c++  java
  • Python读写文件

    1.从一个文件中读取内容(比如有3列内容),然后再新增加2列字段,并存放在列表中(一共5列)。

    2.打开一个新的文件,循环包含有5列表字段的列表,并将内容写进新的文件。

    import random
    
    def writecChannelIdFile(fp, writeFile):
        channel_ids = {'125002805': "135379412029940069", '125102805': "287200733847731473"}  # 125002805:微信 125102805:支付宝
        channel_id = ['125002805', '125102805']
        with open(fp, "r") as ff:
            myReadFile = ff.readlines()
            orderList = []
            for line in myReadFile:
                channel_id_choice = random.choice(channel_id)  #随机取列表的内容(一定要放在for循环里面)
    
                newLine = line.replace("\n", ",")+channel_id_choice +"," + channel_ids[channel_id_choice]
                print(newLine)
                orderList.append(newLine)
    
        with open(writeFile, 'w')as wf:
            for order in orderList:
                print(order)
                wf.write(order+"\n")
    
    if __name__ == "__main__":
        fp = r"D:\xxx\xxx\xxx\a.txt"            #读取该路径下的a.txt文件
        writeFile = r"D:\xxx\xxx\xxx\b.txt"     #会在该路径下生成一个b.txt的文件
        writecChannelIdFile(fp, writeFile)
  • 相关阅读:
    笨办法29IF语句
    笨办法28布尔表达式
    笨办法27记住逻辑
    笨办法26恭喜你,可以进行一次考试了
    笨办法25更多更多的练习
    笨办法23复制文件
    成功转移(✿◡‿◡)
    笨办法22到目前为止你学到了什么?
    vue.js
    webpack-优化
  • 原文地址:https://www.cnblogs.com/tianpin/p/15626286.html
Copyright © 2011-2022 走看看