How can I replace numbered lines with returns in python? -


i have python program takes .txt file list of information. program proceeds number every line, remove returns. want add returns lines numbered without double spacing can continue edit file. here program.

import sys time import sleep  # request filename f = raw_input('filename > ')  print ''  # function opening file load def open_load(text):     c in text:         print c,         sys.stdout.flush()         sleep(0.5)  print "opening file", open_load('...') sleep(0.1)  # loading contents global variable f_ = open(f) f__ = f_.read() # contents contained in variable 'f__' (two underscores)  print f__  raw_input("file opened. press enter number lines or ctrl+c quit. ")  print ''  print "numbering lines", open_load('...') sleep(0.1)  # set below used add numbers lines x = f infile=open(x, 'r') lines=infile.readlines() outtext = ['%d %s' % (i, line) i, line in enumerate (lines)] f_o = (str("".join(outtext))) print f_o  # used show amount of lines open(x) f:     totallines = sum(1 _ in f)  print "total lines:", totallines, "\n"  # -- possible make list of amount of lines use later insert returns? --  raw_input("lines numbered. press enter remove returns or ctrl+c quit. ")  print ''  print "removing returns", open_load('...') sleep(0.1)  # removes instances of return f_nr = f_o.replace("\n", "")  # newest contents located in variable f_nr print f_nr  print ''  raw_input("returns removed. press enter add returns on lines or ctrl+c quit. ")  print ''  print "adding returns", open_load('...') sleep(0.1) 

here example of need. in code, here no returns (\n) in below. have terminal set lines in order without having returns (\n).

1 07/07/15 mcdonalds $20 1 123 12345 2 07/07/15 mcdonalds $20 1 123 12345 3 07/07/15 mcdonalds $20 1 123 12345 4 07/07/15 mcdonalds $20 1 123 12345 5 07/07/15 mcdonalds $20 1 123 12345 

the numbering, 1-5, needs replaced returns each row it's own line. in after being edited

# numbering has been replaced returns (no double spacing) 07/07/15 mcdonalds $20 1 123 12345 07/07/15 mcdonalds $20 1 123 12345 07/07/15 mcdonalds $20 1 123 12345 07/07/15 mcdonalds $20 1 123 12345 07/07/15 mcdonalds $20 1 123 12345 

according input/ouptut data sample:

g = open(output_filename, 'w') f = open(filename) sep = ' '  # separator character  line in f:     l = line.split()[1:]  # remove first item - line number     g.write(sep.join(l))  # rewrite line without  g.close() f.close() 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -