-2

I have a python program that takes a .txt file with a list of information. Then the program proceeds to number every line, then remove all returns. Now I want to add returns to the lines that are numbered without double spacing so I can continue to edit the file. Here is my program.

import sys
from time import sleep

# request for filename
f = raw_input('filename > ')

print ''

# function for opening file load
def open_load(text):
    for c in text:
        print c,
        sys.stdout.flush()
        sleep(0.5)

print "Opening file",
open_load('...')
sleep(0.1)

# loading contents into global variable
f_ = open(f)
f__ = f_.read()
# contents are now contained in variable 'f__' (two underscores)

print f__

raw_input("File opened. Press enter to number the lines or CTRL+C to quit. ")

print ''

print "Numbering lines",
open_load('...')
sleep(0.1)

# set below used to add numbers to lines
x = f
infile=open(x, 'r')
lines=infile.readlines()
outtext = ['%d %s' % (i, line) for i, line in enumerate (lines)]
f_o = (str("".join(outtext)))
print f_o

# used to show amount of lines
with open(x) as f:
    totallines = sum(1 for _ in f)

print "total lines:", totallines, "\n"

# -- POSSIBLE MAKE LIST OF AMOUNT OF LINES TO USE LATER TO INSERT RETURNS? --

raw_input("Lines numbered. Press enter to remove all returns or CTRL+C to quit. ")

print ''

print "Removing returns",
open_load('...')
sleep(0.1)

# removes all instances of a return
f_nr = f_o.replace("\n", "")

# newest contents are now located in variable f_nr
print f_nr

print ''

raw_input("Returns removed. Press enter to add returns on lines or CTRL+C to quit. ")

print ''

print "Adding returns",
open_load('...')
sleep(0.1)

Here is an example of what I need. In my code, here are no returns (\n) in this below. I have the terminal set to where the lines are 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 to be replaced with returns so each row is it's own line. This is what it would look like in after being edited

# the numbering has been replaced with 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
xorist
  • 139
  • 7
  • 2
    You have way too much extraneous code; please try to create a Minimal Verifiable Example (note that you do not need to declare open_load multiple times) – Foon Sep 30 '15 at 13:41
  • 1
    Why do you define `open_load` FOUR TIMES??? One time is enough. And it's better to define function in the beginning of the code. – Psytho Sep 30 '15 at 13:41
  • Tutorial recommendations are off-topic, so I've removed that paragraph (that said: http://sopython.com/wiki/LPTHW_Complaints, http://sopython.com/wiki/What_tutorial_should_I_read%3F) – jonrsharpe Sep 30 '15 at 13:42
  • Thanks for the help. I'm new to stackoverflow.. Still trying to get the hang of the regulations. I've cleared out the repetitive defining of the open_load function. Does anyone have any ideas as to how I can replace the numbers with returns without double spacing? – xorist Sep 30 '15 at 13:47
  • 1
    A pedantic note, but something which might confuse people: a 'return' is `\r` a newline is `\n`. Can you explain why you are removing the newlines in the first place? – cdarke Sep 30 '15 at 13:53
  • Yes. One of my co-workers gets a csv file every month she has to edit by hand. This file can be up to 3000+ lines. So I've been instructed to try and write a program that will do this automatically. Commonly, some lines in this file will have returns in very random places whenever each line just needs to be straight across. – xorist Sep 30 '15 at 13:56
  • Why are you reading the same file 3 times? – PM 2Ring Sep 30 '15 at 14:41
  • It's not easy to understand exactly what you want your code to do. It would help if you include (in code blocks) a small sample of (simulated) input data, and the desired output. – PM 2Ring Sep 30 '15 at 14:57
  • I've added simulated input data and my expected results. – xorist Sep 30 '15 at 15:58
  • Ok. If the data is all supposed to look like that, with each line consisting of a date, a name, a price, and 3 numbers, then it should actually be fairly easy to weed out those stray newlines. But it _will_ take a slightly more sophisticated algorithm than your original approach. – PM 2Ring Oct 01 '15 at 14:33
  • Thanks! I'll just finish learning Pythin and continue after I know more. I'm going to close this question soon. – xorist Oct 01 '15 at 14:36
  • I'll assume the data _is_ always supposed to be like that with 6 "words" per line. You can read the whole file into a single string with `.read()`. You can split that string into a list of separate words with `.split()`. And then you can break that list up into chunks of 6 words each in various ways, as shown in the answers to [How do you split a list into evenly sized chunks in Python?](http://stackoverflow.com/q/312443/4014959) – PM 2Ring Oct 01 '15 at 14:58
  • Thanks! That's exactly what I was looking for. I would uprep your comment if I had the privs. – xorist Oct 01 '15 at 15:01
  • No worries. So now you should have enough info to write a program to do this task properly. :) But it might be a good idea to keep working on the tutorials for a bit longer before you actually try to write this program. Good luck! – PM 2Ring Oct 01 '15 at 15:14

2 Answers2

0

According to your input/ouptut data sample:

g = open(output_filename, 'w')
f = open(filename)
sep = ' '  # separator character

for line in f:
    L = line.split()[1:]  # remove the first item - the line number
    g.write(sep.join(L))  # rewrite the line without it

g.close()
f.close()
JulienD
  • 7,102
  • 9
  • 50
  • 84
  • Thanks for the answer, I tried it and unfortunately it wasn't quite what I was looking for. You see, I numbered the lines and then I took out all of the returns because there are returns in places there shouldn't be within the file. After that I want to replace the numbers I put behind each line (in the sample input it's 1-5) with returns so that all the lines are correct. – xorist Sep 30 '15 at 21:24
0

I've realized that this isn't going to solve my issue. - My issue is that there are returns within the original data of the file that are in the wrong place. So I wrote a program to number every line before I took out all of the returns, then I wanted to replace the numbers in front of the lines with new returns in order to make everything correct. Thanks for all you guy's help! I should've seen that before I even started, lol.

xorist
  • 139
  • 7