greyoxide
greyoxide

Reputation: 1287

Python- need to append characters to the beginning and end of each line in text file

I should preface that I am a complete Python Newbie.

Im trying to create a script that will loop through a directory and its subdirectories looking for text files. When it encounters a text file it will parse the file and convert it to NITF XML and upload to an FTP directory.

At this point I am still working on reading the text file into variables so that they can be inserted into the XML document in the right places. An example to the text file is as follows.

Headline
Subhead
By A person
Paragraph text.

And here is the code I have so far:

with open("path/to/textFile.txt") as f:
    #content = f.readlines()
    head,sub,auth = [f.readline().strip() for i in range(3)]
    data=f.read()
pth = os.getcwd()

print head,sub,auth,data,pth

My question is: how do I iterate through the body of the text file(data) and wrap each line in HTML P tags? For example;

<P>line of text in file </P> <P>Next line in text file</p>.

Upvotes: 0

Views: 2449

Answers (7)

martineau
martineau

Reputation: 123443

You could use the fileinput module to modify one or more files in-place, with optional backup file creation if desired (see its documentation for details). Here's it being used to process one file.

import fileinput

for line in fileinput.input('testinput.txt', inplace=1):
    print '<P>'+line[:-1]+'<\P>'

The 'testinput.txt' argument could also be a sequence of two or more file names instead of just a single one, which could be useful especially if you're using os.walk() to generate the list of files in the directory and its subdirectories to process (as you probably should be doing).

Upvotes: 0

user695580
user695580

Reputation: 41

append the

and <\p> for each line

ex:

     data_new=[]
     data=f.readlines()
     for lines in data:
          data_new.append("<p>%s</p>\n" % data.strip().strip("\n"))

Upvotes: 0

mgilson
mgilson

Reputation: 309891

with open('infile') as fin, open('outfile',w) as fout:
    for line in fin:
        fout.write('<P>{0}</P>\n'.format(line[:-1])  #slice off the newline.  Same as `line.rstrip('\n')`.

#Only do this once you're sure the script works :)
shutil.move('outfile','infile')  #Need to replace the input file with the output file

Upvotes: 1

Jon Clements
Jon Clements

Reputation: 142136

Something like

output_format = '<p>{}</p>\n'.format
with open('input') as fin, open('output', 'w') as fout:
    fout.writelines( output_format(line.strip()) for line in fin )

Upvotes: 3

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250921

use data=f.readlines() here,

and then iterate over data and try something like this:

for line in data:
   line="<p>"+line.strip()+"</p>"
   #write line+'\n' to a file or do something else 

Upvotes: 0

mata
mata

Reputation: 69022

in you case, you should probably replace

data=f.read()

with:

data = '\n'.join("<p>%s</p>" % l.strip() for l in f)

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208455

This assumes that you want to write the new content back to the original file:

with open('path/to/textFile.txt') as f:
    content = f.readlines()

with open('path/to/textFile.txt', 'w') as f:
    for line in content:
        f.write('<p>' + line.strip() + '</p>\n')

Upvotes: 1

Related Questions