Reformating an external file and then saving it, from inside a python code
Sorry for the basic question, but my python is a bit rusty after having not worked with it for a few months!
Basically I am trying to run a python code over a li开发者_如何学JAVAst of files which are the output of another code (which unfortunately I cannot alter). The problem is that the code I have written requires the files in "input.txt" to all be on separate lines and currently they are separated by a comma.
Try as I might I haven't been able to write anything that reformats this file inside my code.
The files currently look like:
file1,file2,file3
and I need to get them to look like:
file1
file2
file3
and then re save "input.txt" before using it in:
for ln in open("input.txt"):
ch.Add(ln.strip())
(where ch is a chain)
Any help would be much appreciated, thanks!
def generateFromFile ( fileName ):
for line in open( fileName ):
for segment in line.split( ',' ):
yield segment
for ln in generateFromFile( "input.txt" ):
ch.Add( ln.strip() )
for x in files.split(','):
print >> input, x
精彩评论