python - add delimiter to fixed-width text file -


I am trying to add a delimiter to a fixed-width text file.

This is exactly what I'm far away from: Output as Open ('output.txt', 'w'): ('<'> with list = [4] , 29,36,36,45,70,95,100,111,115,140,150,151,152,153,169] Inputetyxt ',' r ') as infile: for the line in infile: newline = line [: 4] +' | '+ Line [4:] outfile.write (newline) outfile.close ()

The above code inserts a pipe at 5 bytes. Now I want to add a pipe to the list at the next price (29). I'm using Python 2.7.

I think this is what you want to do:

  list = [4,29,36, 36,45,70,95,100,111,115,140,150,151,152,153,169] R ') as: for the line in the infile: iter = 0 prev_position = 0 position = list [iter] temp = [] while the position is & lt; Lane (line) and IARA + 1 & lt; Len (list): iter + = 1 temp.append (line [prev_position: position]) prev_position = position position = list [iter] temp.append (line [prev_position:]) temp_str = '.join (x + "| Temp_str = temp_str [: - 1] outfile.write (temp_str)  

This takes an input file and a | Within the list, those cases will be handled which are smaller or larger than the prices in your list.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -