Skip to content
Advertisement

Append Text (Single Letter) to the end of each line in a text file

Below is an example of the text file I am working with:

JavaScript

I am trying to add simply the letter ‘S’ to the end of every other line. So, above, there 3 total records. Right after 05/29/2014, I want to insert the S. So a every record would look like:

JavaScript

I realize this would be Oh so simple converting to CSV and working with excel, but I’m getting all sorts of formatting issues on the transfer back to txt. Wanted to take a crack at it with python. I’m trying to use append, from what I understand, write will overwrite my existing file:

JavaScript

I don’t use python often, I’m wondering how I can index it so it starts with line 2, and appends the very end of the line after the comma, like I noted above.

Advertisement

Answer

You’ll need to read the file line by line and then output line by line again. This is much simpler than using CSV or even spread sheet processing software which truly scares me.

JavaScript

If you are still using Python 2, use

JavaScript

instead of the print.

Update: If you want to append to every (as opposed to every other) line, simply use:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement