用python去掉每一行前面的空格
lstrip() 方法用于截掉字符串左边的空格或指定字符。
去掉a.txt中每一行的最前面的空格,并将处理后的内容写进b.txt中:
with open("d:\\a.txt",'r',encoding="utf-8") as read_txt:
with open("d:\\b.txt",'w',encoding="utf-8") as write_txt:
for line in read_txt.readlines():
write_txt.write(line.lstrip())
注意:如果a.txt中有空行,处理后的数据会少相应的行数;如果要保持行数不变,建议在空行处写入N,防止处理后的数据少行。