tags:

views:

148

answers:

4

In the actual window where I right code is there a way to insert part of the code into everyline that I already have. Like insert a comma into all lines at the first spot>?

A: 

Code is data. You could do this like you would with any other text file. Open the file, read the line, stick a comma on the front of it, then write it back to file.

Also, most modern IDEs/text editors have the ability to define macros. You could post a question asking for specific help for your editor. For example, in Emacs I would use C-x ( to start defining a macro, then ',' to write a comma, then C-b C-n to go back a character and down a line, then C-x ) to end my macro. I could then run this macro with C-x e, pressing e to execute it an additional time.

Wilduck
I need to make changes to 90,000 lines in my new window in python
novak
And my answer still applies... In emacs, C-u (number) C-x e will run your macro (number) times.
Wilduck
I don't understand that?
novak
My first sentence is asking you to do something very similar to what I explained to you in http://stackoverflow.com/questions/3207719/question-about-splitting-a-large-file this question. This is not the easiest way to do that, but if you don't understand what emacs or a macro is, I'm not going to try to explain them. That's what google is for.
Wilduck
Thats not all that helpful to me wilduck.
novak
Yes it is. You just don't understand it.
Wilduck
Oh Wilduck I'm sure I have more advanced degrees than you. When you decide to teach yourself neurosurgery on your own we can talk. I think I'm doing a decent job for a weeks worth of experience in programming.
novak
I can't believe I'm getting into a flame war on stackoverflow comments. Still, just so you know, if I ever had a question for a neurosurgeon, I would make darn sure that I was able to articulate myself before I asked it. Mostly, though, I wouldn't be asking questions if I only had a week's worth of experience with neurosurgery. I would be reading an introductory textbook. You should do the same for python. Nick T gave you a link.
Wilduck
Wilduck english is my 3rd language. Would you prefer I speak in portugese?
novak
No, actually you're english is very good in these comments. If you're comfortable in spanish however, we could communicate that way. I don't want to insult you, but I do think it would be in your best interest to read an introductory python book/tutorial, as well as the expectations of this site. I'm sorry that I couldn't be helpful for this question.
Wilduck
No hard feelings Wilduck I will be using these forums less and lessly hopefully as I become better suited to the tasks at hand.
novak
+1  A: 

Are you talking about the interactive shell? (a.k.a. opening up a prompt and typing python)? You can't go back and edit what those previous commands did (as they have been executed), but you can hit the up arrow to flip through those commands to edit and reexecute them.

If you're doing anything very long, the best bet is to write your program into your text editor of choice, save that file, then launch it.

Adding a comma to the start of every line with Python:

import sys
src = open(sys.argv[1])
dest = open('withcommas-' + sys.argv[1],'w')
for line in src:
    dest.write(',' + line)
src.close()
dest.close()

Call like so: C:\Scripts>python commaz.py cc.py. This is a bizzare thing to do, but who am I to argue.

Nick T
I open python and click new window. I have my program written there but its 90,000 lines long. I want to insert a correction and dont want to mannually do it 90,000 times
novak
@novak What IDE are you using? If you want to do some basic tweak to every line (add a comma) you could do that in a text editor with search/replace or several other ways.
Nick T
I have wingding
novak
@novak I have no knowledge of that editor; if you have *nix or Cygwin use one of the other answers; if you're living on Windows, there are still many better options (I like Notepad++) then my code fragment above, but it works.
Nick T
i'm totally confused by what you are saying
novak
@novak 1. Save the above code as `commaz.py` in the same directory as your target file. 2. Open command prompt and `cd` to that directory. 3. Run `python commaz.py [yourfile]`.
Nick T
what about if i wanted to add a comma to the end of every line?
novak
also withcommas should be replaced by the name of my file right?
novak
@novak I like your interest in learning Python. You should start with some of the basic tutorials to become familiar with the language. http://wiki.python.org/moin/BeginnersGuide
Nick T
@novak: google doesn't seem to have any knowledge of a wingding IDE or editor
John Machin
+1  A: 

If you are in UNIX environment, open up a terminal, cd to the directory your file is in and use the sed command. I think this may work:

sed "s/\n/\n,/" your_filename.py > new_filename.py

What this says is to replace all \n (newline character) to \n, (newline character + comma character) in your_filename.py and to output the result into new_filename.py.


UPDATE: This is much better:

sed "s/^/,/" your_filename.py > new_filename.py

This is very similar to the previous example, however we use the regular expression token ^ which matches the beginning of each line (and $ is the symbol for end).


There are chances this doesn't work or that it doesn't even apply to you because you didn't really provide that much information in your question (and I would have just commented on it, but I can't because I don't have enough reputation or something). Good luck.

tjko
I don't have unix.
novak
Maybe you should have mentioned this in your question? It is much simpler for people to help you if you don't put the bare minimum amount of effort into receiving help. Less typos and more details when posing a question will go a long way. In any case, good luck finding the answer you are looking for.
tjko
+2  A: 

You need a file editor, not python.

  1. Install the appropriate VIM variant for your operating system
  2. Open the file you want to modify using VIM
  3. Type: :%s/^/,/
  4. Type: :wq
MikeyB
No fair. VIM is too complicated for neurosurgeons (check the comments to Wilduck's answer).
Tim Pietzcker