Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Simple newb string question
#1
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")
Quote
#2
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'{'.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
Thanks! That did it.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Simple question concerning python dot notation. miner_tom 1 63 Mar-24-2020, 05:20 PM
Last Post: buran
  Simple question about .format(* ) Motley_Cow 1 200 Oct-03-2019, 07:13 AM
Last Post: buran
  string parsing question ( I think) diggee17 4 398 Jul-24-2019, 02:37 PM
Last Post: diggee17
  simple string & input problem kungshamji 5 509 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  please help this newb install pygame iofhua 7 1,152 May-15-2019, 01:09 PM
Last Post: buran
  Newb question: Debugging + Linting Python in Visual Studio Code Drone4four 1 440 Apr-15-2019, 06:19 AM
Last Post: perfringo
  simple syntax question speedskis777 7 776 Mar-11-2019, 02:26 AM
Last Post: snippsat
  Newb question about %02d %04d bennylava 30 4,486 Mar-05-2019, 11:23 PM
Last Post: snippsat
  Pthyon 3 question (newb) bennylava 11 1,129 Feb-28-2019, 06:04 PM
Last Post: buran
  please help with this question about using operators to multiply a string? GilesTwigg 3 584 Feb-27-2019, 04:13 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)