0

I am trying to read in a python file and split the lines into elements of an array. I am able to read and split the file content, however sometimes the leading white space for some of the elements gets removed.

Is there a way to split the file while making sure to retain all of the white spaces?

Here is my code to read in the file:

reader = new FileReader();
reader.onload = function (progressEvent)
    var lines = file.split("\n");

Here is an example input:

def main():
    print(1)
    print(2)
def main2():
    print(3)
    #test

Here is the expected output:

0: "def main():"
1: "    print(1)"
2: "    print(2)"
3: "def main2():"
4: "    print(3)"
5: "    #test"

Here is the real output:

0: "def main():"
1: "print(1)"
2: "print(2)"
3: "def main2():"
4: "print(3)"
5: "#test"
eli0tt
  • 677
  • 1
  • 7
  • 19
Hunter
  • 110
  • 1
  • 9
  • You're probably displaying the output in a way that ignores whitespace. – SLaks Nov 22 '17 at 15:07
  • If you are just wanting to display the text then wrap it in a
     element.
    –  Nov 22 '17 at 15:08
  • 1
    Have you tried reading the lines [one at a time](https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js)? – Olian04 Nov 22 '17 at 15:09
  • How do you read the file? Using `readAsText()` seems to be working as expected: https://codepen.io/anon/pen/ZaoOEY - Base code from: http://blog.teamtreehouse.com/reading-files-using-the-html5-filereader-api (Forgot: Inspect element->Console to see the array being printed) – urban Nov 22 '17 at 15:47

0 Answers0