我正在尝试读取python文件并将行拆分为数组元素。我能够读取和拆分文件内容,但有时会删除某些元素的前导空格。
有没有办法分割文件,同时确保保留所有空格?
以下是我在文件中阅读的代码:
reader = new FileReader();
reader.onload = function (progressEvent)
var lines = file.split("\n");
以下是输入示例:
def main():
print(1)
print(2)
def main2():
print(3)
#test
这是预期的输出:
0: "def main():"
1: " print(1)"
2: " print(2)"
3: "def main2():"
4: " print(3)"
5: " #test"
这是真正的输出:
0: "def main():"
1: "print(1)"
2: "print(2)"
3: "def main2():"
4: "print(3)"
5: "#test"