0

I'm trying to open a file, and edit a specific line. When I concatenate a character onto one of the lines, it works, but inserts a new line. However I don't want a new line. Here is the code:

def moveCurlyInline(line, i):
    with open('test.js', 'r') as inputFile:
        data = inputFile.readlines()
        print(data[0])
        print(data[0] + ' {')

The print outputs:

function hello()

then:

function hello()
 {

I need the curly bracket to be on the same line as the function hello. Any idea what's wrong with my code?

dwib
  • 583
  • 4
  • 19

2 Answers2

1

f.readline() reads a line from the file, including the newline at the end of the line.

Try stripping the extra newline:

data = [line.rstrip("\n") for line in inputFile]
iBug
  • 35,554
  • 7
  • 89
  • 134
1

You can strip new line character by

inputFile.read().striplines()

mad_
  • 8,121
  • 2
  • 25
  • 40