0

I have a text file like this:-

V1xx AB1
V2xx AC34
V3xx AB1

Can we add ; at each end of line through python script?

V1xx AB1;
V2xx AC34;
V3xx AB1;
5
  • Do you want to create a new file, or write it to existing file? Commented Oct 16, 2012 at 22:16
  • I want to write it in the existing file.... Commented Oct 16, 2012 at 22:18
  • new file is also OK, as long as it has all the data Commented Oct 16, 2012 at 22:21
  • 2
    Yes, it's trivial to add a semicolon to each line and write it to a new file. What part are you having trouble with? Commented Oct 16, 2012 at 22:25
  • 1
    What problem are you facing? You can iterate over all lines, add a ; to them and append to a new file? Commented Oct 16, 2012 at 22:34

3 Answers 3

1

Here's what you can try. I have overwritten the same file though.

You can try creating a new one(I leave it to you) - You'll need to modify your with statement a little : -

lines = ""

with open('D:\File.txt') as file:
    for line in file:
        lines += line.strip() + ";\n"

file = open('D:\File.txt', "w+")
file.writelines(lines)

file.flush()

UPDATE: - For in-place modification of file, you can use fileinput module: -

import fileinput

for line in fileinput.input('D:\File.txt', inplace = True):
    print line.strip() + ";"
Sign up to request clarification or add additional context in comments.

3 Comments

Fileinput module supports in-place modification of file allthough appears that the OP is looking to have things served up on a platter
@cravoori Thanks for the information. Can you take a look at my added code. Is it correct way of doing it?
Thanks, it works. I was using perl and that too worked as well.
1
input_file_name = 'input.txt'
output_file_name = 'output.txt'

with open(input_file_name, 'rt') as input, open(output_file_name, 'wt') as output:
    for line in input:
        output.write(line[:-1]+';\n')

Comments

0
#Open the original file, and create a blank file in write mode
File     = open("D:\myfilepath\myfile.txt")
FileCopy = open("D:\myfilepath\myfile_Copy.txt","w")

#For each line in the file, remove the end line character,
#insert a semicolon, and then add a new end line character.
#copy these lines into the blank file
for line in File:
    CleanLine=line.strip("\n")
    FileCopy.write(CleanLine+";\n")
FileCopy.close()
File.close()

#Replace the original file with the copied file
File = open("D:\myfilepath\myfile.txt","w")
FileCopy = open("D:\myfilepath\myfile_Copy.txt")
for line in FileCopy:
    File.write(line)
FileCopy.close()
File.close() 

Notes: I have left the "copy file" in there as a back up. You can manually delete it or use os.remove() (if you do that don't forget to import the os module)

1 Comment

Thanks, it works. I was using perl and that too worked as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.