python - How do I concatenate lines from a file onto a single line of code? -
i'm new programming , working on problem #269 r/dailyprogrammer. want add ability read files , format them based on indention format given. current code:
import sys filename=input("what file reformat?") f=open(filename,"r") n_lines= int(f.readline()) indents=(f.readline()) block=0 line in f: line=line.lstrip('·» \t') if line.startswith('endif') or line.startswith('next'): block-=1 print(indents*block+line) if line.startswith('for') or line.startswith('if'): block+=1
gives me this
var i=1 31 ···· if !(i mod 3) ···· ···· print "fizz" ···· endif ···· if !(i mod 5) ···· ···· print "buzz" ···· endif ···· if (i mod 3) && (i mod 5) ···· ···· print "fizzbuzz" ···· endif next
,but want print pseudo indentions+line on same line.
your indents
string ends newline, this: " \n"
. want rid of that.
indents=f.readline().strip('\n')
when reading text file, there newline character @ end of each line until you've reached end of file.
print()
add single newline output default. if don't want that, can pass end
argument.
print('this line doesn't need newline.\n', end='')