Aug-08-2018, 02:25 PM
(This post was last modified: Aug-08-2018, 02:45 PM by morgandebray.)
Hello, I'm having a (little) issue by adding a new line. The newline is added before the line I want.
See my code
Solution :
See my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
for fichier in os.listdir( "result/" ): if not fnmatch.fnmatch(fichier, 'file_0.txt' ): # print(fichier) with fileinput. input ([ "result/" + fichier], inplace = 1 ) as file_X : print ( "---------------------------------" ) for line in file_X: line = line.strip( '\n' ) if line.startswith( "/54/" ): split = line.split( "/" ) num = split[ 2 ] for pj in pjCSV: if num in pj: ... sys.stderr.write(new_name + "\r\n" ) if line.startswith( '/22/' ): sys.stderr.write(line) print ( "coucou" ) # this is added before the line starting with /22/, I want after print (line.rstrip( '\n' )) # break |
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
for fichier in os.listdir( "result/" ): if not fnmatch.fnmatch(fichier, 'file_0.txt' ): with fileinput. input ([ "result/" + fichier], inplace = 1 ) as file_X : # file_X.write('\r\ncoucou') print ( "---------------------------------" ) for line in file_X: line = line.strip( '\n' ) if line.startswith( "/54/" ): split = line.split( "/" ) num = split[ 2 ] # print(num) for pj in pjCSV: if num in pj: ... sys.stderr.write(new_name + "\r\n" ) if line.startswith( '/22/' ): print (line.replace(line, line + "\r\n" )) continue print (line.rstrip( '\n' )) # ecriture de toutes les lignes break |