Jesus Alonso
Jesus Alonso

Reputation: 1

How to fix this line breaks?

Im creating a new file which has the data from other file, and i have to add extra information. The code runs okay but when i open the file the data that i add messes up the file structure.

def writeSalary(fileName):
    f = open(fileName,"r")
    g = open("Personas2.txt","w")
    for line in f:
        line=line[:-1]
        g.write(line)
        g.write("1000\n")
    f.close()
    g.close()

The expected output is:

Pedro Gonzalez Diaz 71977689X 22 M ESP EDP 1000
Juan Gonzalez Diaz 71977689X 21 M ITA CSC 1000
Federico Gonzalez Diaz 71977689X 32 M ITA TELEFONICA 1000
Javier Gonzalez Diaz 71977689X 35 M ITA TELEFONICA 1000
Manuel Gonzalez Diaz 71977689X 27 M ESP EDP 1000
Pelayo Gonzalez Diaz 71977689X 60 M ESP TELEFONICA 1000
Luis Gonzalez Diaz 71977689X 45 M GER CSC 1000
Ana Gonzalez Diaz 71977689X 30 F ESP CSC 1000
Caterina Gonzalez Diaz 71977689X 24 F ITA CSC 1000
Isabel Gonzalez Diaz 71977689X 29 F ESP TELEFONICA 1000
Covadonga Gonzalez Diaz 71977689X 18 F ESP TELEFONICA 1000
Sonia Gonzalez Diaz 71977689X 66 F ESP EDP 1000
Alejandra Gonzalez Diaz 71977689X 20 F ITA EDP 1000
Pedro Gonzalez Diaz 71977689X 20 M ITA ED 1000

But it is:

Pedro Gonzalez Diaz 71977689X 22 M ESP EDP 1000
1000
Juan Gonzalez Diaz 71977689X 21 M ITA CSC 1000
1000
Federico Gonzalez Diaz 71977689X 32 M ITA TELEFONICA 1000
1000
Javier Gonzalez Diaz 71977689X 35 M ITA TELEFONICA 100
1000
Manuel Gonzalez Diaz 71977689X 27 M ESP EDP 1000
1000
Pelayo Gonzalez Diaz 71977689X 60 M ESP TELEFONICA 1000
1000
Luis Gonzalez Diaz 71977689X 45 M GER CSC 1000
1000
Ana Gonzalez Diaz 71977689X 30 F ESP CSC 1000
1000
Caterina Gonzalez Diaz 71977689X 24 F ITA CSC 1000
1000
Isabel Gonzalez Diaz 71977689X 29 F ESP TELEFONICA 1000
1000
Covadonga Gonzalez Diaz 71977689X 18 F ESP TELEFONICA 1000
1000
Sonia Gonzalez Diaz 71977689X 66 F ESP EDP 1000
1000
Alejandra Gonzalez Diaz 71977689X 20 F ITA EDP 1000
1000
Pedro Gonzalez Diaz 71977689X 20 M ITA ED 1000

Upvotes: 0

Views: 55

Answers (1)

grapes
grapes

Reputation: 8636

Just change line=line[:-1] to line=line.rstrip().

Upvotes: 1

Related Questions