python - How to replace the line in which I'm currently? -


i have python code this:

with open('myfile') f:         next(f) # skip first line         line in f:             items = line.split(';')             if len(items) < 2:                 # want replace line s.th. write 

how replace line s.th. want write?

open file in r+ mode, read it's content in list first , after truncation can write new data file.

'r+' opens file both reading , writing

if file huge better write new file first , rename.

with open('myfile','r+') f:         data=f.readlines()[1:]         f.truncate(0)          #this truncate file         f.seek(0)             #now file pointer goes start of file         line in data:     #now write new data             items = line.split(';')              if len(items) < 2:                 # here             else:                 f.write(line) 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -