Replace a word in a file

后端 未结 4 1589
被撕碎了的回忆
被撕碎了的回忆 2021-01-23 04:36

I am new to Python programming...

I have a .txt file....... It looks like..

0,Salary,14000

0,Bonus,5000

0,gift,6000

I want to to replace

4条回答
  •  一整个雨季
    2021-01-23 05:13

    o=open("output.txt","w")
    for line in open("file"):
        s=line.split(",")
        s[0]="1"
        o.write(','.join(s))
    o.close()
    

    Or you can use fileinput with in place edit

    import fileinput
    for line in fileinput.FileInput("file",inplace=1):
        s=line.split(",")
        s[0]="1"
        print ','.join(s)
    

提交回复
热议问题