Discuss Scratch

-ScratchOs
Scratcher
71 posts

FIXED P̶y̶t̶h̶o̶n̶ ̶n̶e̶w̶ ̶l̶i̶n̶e̶ ̶c̶h̶a̶r̶a̶c̶t̶e̶r̶

FIXEDI writing something in python that needs to loop through a file, treating new lines and spaces identically, but currently it does not work for new lines.

For Example:
input = "hello" "hi"; print print
output = ['hello',hi'] and ['print','print']
however, the input
"hello"
"hi";
print
print
should produce an identical output, just using newline instead of space, however it produces
['hellohi'] and ['printprint']

So my code is not recognizing new lines in the same way as the spaces.

def run(file):
with open(file) as prog:
part = 1
line = ""
qu = 0
args = []
exe = []
data = prog.read().replace('\n', '')
for x in range(0,len(data)):
if data[x] == "\"":
if qu == 0:
print("qu = 1")
qu = 1
else:
print("qu = 0")
print(line)
qu = 0
else:
if (data[x] == " " or data[x] == "\n" or data[x] == ";") and qu != 1: #here the '\n' is meant to be identical to the ' ' but it is not.
if part == 1:
print("part = 1")
if line != "":
args.append(line)
elif part == 2:
print("part = 2")
if line != "":
exe.append(line)
line = ""
else:
line = line + data[x]
if data[x] == ";":
print("part = 2")
part = 2
if part == 1:
args.append(line)
elif part == 2:
exe.append(line)
print(args)
print(exe)

can you help with this?

Last edited by -ScratchOs (Oct. 29, 2015 15:29:30)

Ethan_1
Scratcher
100+ posts

FIXED P̶y̶t̶h̶o̶n̶ ̶n̶e̶w̶ ̶l̶i̶n̶e̶ ̶c̶h̶a̶r̶a̶c̶t̶e̶r̶

So, learning Python? What do you like/not like?

Last edited by Ethan_1 (Oct. 30, 2015 02:41:47)

Powered by DjangoBB