Python Forum
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")
Reply
#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
Reply
#3
Thanks! That did it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  String to List question help James_Thomas 6 921 Sep-06-2023, 02:32 PM
Last Post: deanhystad
  Simple Question - ' defined as "a". ?' Ryan012 10 1,491 May-27-2023, 06:03 PM
Last Post: Ryan012
  syntax error question - string mgallotti 5 1,251 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Very simple question about filenames and backslashes! garynewport 4 1,832 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,475 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
Big Grin General programming question (input string)[ jamie_01 2 1,569 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
  Simple arithmetic question ebolisa 5 2,008 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,238 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,635 Oct-04-2021, 11:55 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020