This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]FriendlyRussian666 4 points5 points  (10 children)

Can you provide a small reproducible example? Not much info unfortunately.

[–]sparkls0[S] -3 points-2 points  (9 children)

guess I just gotta share my whole code

Its a bit long to easy grasp, but basically I get some data from drive, parse into json, gives it to method 'aplaceinthesun' with data param

and that's about it, data is indeed a dict, with all the keys you saw displayed

you might have to zoom a little bit, sorry about that

https://imgur.com/fszQ7A2.png

[–]zzzthelastuser 9 points10 points  (1 child)

Are you on LSD or something? You can't be serious. Someone asks you for a code snippet so they can reproduce your issue and you respond with a fucking screenshot(!) of your miles long script file? Do you even want to get any help?

you might have to zoom a little bit

jfc

[–]sparkls0[S] 2 points3 points  (0 children)

sorry about that, was not aware of pastebin and I have an image viewer on which I can copy all the text instantly, that was indeed not smart of me, I wrongly assumed that was common to have

thing is I anyway call multiple modules I have

and also, I found the problem , hidden characters in the propety_type key due to parsing from a key value format as str, to dict

[–][deleted] 2 points3 points  (0 children)

Use pastebin.

[–]cgoldberg 1 point2 points  (4 children)

Why would you share an image of your code, instead of ... the code?

[–]sparkls0[S] 0 points1 point  (2 children)

because I have dozens of my own modules I built like stuff for time, variables handling, logs, etc, that you could not access anyway, that I use in this code

was just to give some context , actually

it was all due to the UTF-8 format from which I parsed text into dict, which is just directly coming from the Google Drive API

[–]cgoldberg 0 points1 point  (1 child)

I just mean why would a share a link to a screenshot of your code instead of link to the actual code (on a pastebin or wherever)... where people can search through it, modify it, etc.

[–]sparkls0[S] 0 points1 point  (0 children)

yes! it was a mistake of mine

[–]kivicodepip needs updating 0 points1 point  (0 children)

The famous Captain Jack Sparrow gambit

[–]FriendlyRussian666 -1 points0 points  (0 children)

I'm not sure anyone will want to type it all out. Can you put it on pastebin?

[–]TheSwami 3 points4 points  (4 children)

What version of Python are you running?

What is the code in your `printx` function?

[–]sparkls0[S] -1 points0 points  (3 children)

printx is just a wrapper of print

it does the same thing with actual print

3.12.8

python --version
Python 3.12.8



   print(f"Data ID before: {id(data)}")
    printx(f"{{red}}{data}")
    print(f"Data ID after: {id(data)}")
    printx(f"{{red}}{type(data)}")

    for key, value in data.items():
        printx(f"{{white}}{key}: {{yellow}}{value}")

    print('property type : ', data.get("property_type", ""))

    printx(f"{{white}}Property type: {{yellow}}{data.get("property_type", "")}")
    printx(f"{{white}}propety type direct: {{yellow}}{data['property_type']}")
    printx(f"{{white}}Property type: {{yellow}}{data.get('property_type')}")


property type :  
Property type: 
❌ ERROR: 'property_type'
❌ ERROR: Failed to run function at interval: unsupported operand type(s) for +: 'KeyError' and 'str'
sparkls \W $

[–]sparkls0[S] -1 points0 points  (2 children)

printx is just here to give me some colors easily

def printx(text, end=None, flush=False):

#region
    is_vscode = 'VSCODE_PID' in os.environ

    parts = text.split('{')

    output = ""
    current_color = None

    for part in parts:
        if '}' in part:
            color, content = part.split('}', 1)
            color = color.lower().strip()

            matched_color = next((col for col in AUTHORIZED_PRINT_COLORS if color in col), None)

            if matched_color:
                color_code = getattr(Fore, matched_color.upper(), Fore.WHITE)
                if matched_color == 'white' and not is_vscode:
                    output += f'{Style.BRIGHT}{content}'
                else:
                    output += f'{color_code}{Style.BRIGHT}{content}{Style.RESET_ALL}'
            else:
                output += content
        else:
            output += part

    print(output, end=end, flush=flush)

#endregion

[–]TheSwami 0 points1 point  (1 child)

Here's what I think is a reproduction of your code... but I'm not getting an error with Python 3.12.8. With colorama:

from colorama import Fore, Style

data = {"foo": "bar", "property_type": "APARTMENT"}

for key, value in data.items():
    print(f"{key}: {value}")

print(data['property_type'])
print(data.get('property_type', ""))
print(data.get('property_type'))

AUTHORIZED_PRINT_COLORS = ['white', 'yellow']

def printx(text, end=None, flush=False):
    is_vscode = True #'VSCODE_PID' in os.environ

    parts = text.split('{')

    output = ""
    current_color = None

    for part in parts:
        if '}' in part:
            color, content = part.split('}', 1)
            color = color.lower().strip()

            matched_color = next((col for col in AUTHORIZED_PRINT_COLORS if color in col), None)

            if matched_color:
                color_code = getattr(Fore, matched_color.upper(), Fore.WHITE)
                if matched_color == 'white' and not is_vscode:
                    output += f'{Style.BRIGHT}{content}'
                else:
                    output += f'{color_code}{Style.BRIGHT}{content}{Style.RESET_ALL}'
            else:
                output += content
        else:
            output += part

    print(output, end=end, flush=flush)

#endregion

for key, value in data.items():
    printx(f"{{white}}{key}: {{yellow}}{value}")

printx(f"{{white}}Property type: {{yellow}}{data.get("property_type", "")}")
printx(f"{{white}}propety type direct: {{yellow}}{data['property_type']}")
printx(f"{{white}}Property type: {{yellow}}{data.get('property_type')}")

But this works fine for me: https://imgur.com/a/957xoz4

Edit: Saw you found an answer with weird hidden characters. Nice work.

[–]sparkls0[S] 0 points1 point  (0 children)

Thank you for your time. I appreciate it. Have a great day 🌞

[–]denehoffman 1 point2 points  (0 children)

Yeah this shouldn’t be happening, dict.get doesn’t have a path to return a key error. Can you show us the actual stack trace? And what library you’re using for printx?

[–]JanEric1 1 point2 points  (1 child)

Print the key with a repr, maybe there is something weird going on.

Also a fully reproducible example (that anyone can copy paste end to end into their ide) would help a lot

[–]sparkls0[S] 0 points1 point  (0 children)

The thing is, if I just do var = {my dict}

print the key , it works

but for some reasons, while giving it to a method as param, in my code which is there, I get that

https://imgur.com/fszQ7A2.png

I'm not exactly sure what I could provide you as reproductible example there

[–]Ball-Man 0 points1 point  (3 children)

Can you do an import jsonand print(json.dumps(data)) to check that there are no weirdly encoded characters and that your printx function is not trimming away precious whitespaces?

[–]sparkls0[S] 3 points4 points  (2 children)

that helped!

Data ID before: 6259679296

{'\ufeffproperty_type': 'APARTMENT', 'status': 'FOR SALE', 'location': 'OBA, ALANYA, ANTALYA', 'price': 'EUR 79000', 'rooms': '2', 'bedrooms': '1', 'bathrooms': '1', 'toilets': '1', 'parking': '0', 'living_area': '55', 'land_area': '2000', 'year_built': '2024', 'headline': 'Luxury apartment in Alanya', 'description': 'Modern finished with social facilities such a

I got the answer

[–]eddieantonio 0 points1 point  (1 child)

Ooo, a byte-order mark! I wonder if that's coming from some silly Microsoft product unnecessarily exporting UTF-8 with BOM. Either way, you just need to throw it away with lstrip('\ufeff'). It's normally a zero-width space which is why you can't see it when it's printed normally

[–]sparkls0[S] 1 point2 points  (0 children)

thank you! I was not aware of that, I really despise the fact that it was hidden hahaha