Simple newb string question - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Simple newb string question (/thread-20948.html) |
Simple newb string question - Involute - Sep-07-2019 In the following code I open two files (one to read from and the other to write to). I want to read through f until I get to a left brace ("{") before I start writing out to g. The left brace is there, but the code never executes the first break. The output of print(x) looks like this on the console: b'=' b' ' b'{' b'\r' b'\n' b'\t' b'0' You can see the brace in the third line. I've tried a number of formats for the right side of the == in the if str(x) == '{': line, but I still can't get it to break. What am I doing wrong? Thanks. with open("E:\\Karl\\Documents\\Sculpture\\Prime Time\\Digits\\1d_inv_conv.c", 'w') as g: with open("E:\\Karl\\Documents\\Sculpture\\Prime Time\\Digits\\1d_inv.c", 'rb') as f: while True: x = f.read(1) print(x) if str(x) == '{': break j = 1 while (j == 1): for i in range(5): x = f.read(1) if x == '': j = 0 break # print(x) g.write(str(x)) g.write(", ") g.write("\n") RE: Simple newb string question - ichabod801 - Sep-07-2019 The string of a bytestring is still a bytestring and is not equal to the string with the same characters. Instead of testing str(x) == '{' , try testing x == b'{' .
RE: Simple newb string question - Involute - Sep-08-2019 Thanks! That did it. |