GhPython : struggling with accents

Spoiler : I’m a huge Python noob

Working on some GhPython script to extract data from a SQLite database.

In the Python component’s preview, I get the string results of my queries just fine, but when the output is displayed in a panel, all the accents get the unicode treatment.

I’ve tried to interpret solutions found here and there, but with no success.

How can I do ?

try not to use any special characters and could you show us the the output “out” (screenshot of the panel) thanks

Hi Clementslinder,

Sorry, I am plagued with being French :slight_smile:

1 Like

Thank you! Don´t worry french is a great language :wink:
In the screenshot, you provided above. Isn´t it what you´re looking for in the “out” output?

Clemens

Yes it is, except I want to enter multiple SQL queries, and for each, get a distinct output.
Therefore, I need to fix this encoding issue.

You probably need to upload a more complete minimal (non-)working example. I can output a string with french accents just fine on my system:


210114_FrenchAccents_00.gh (2.3 KB)

But also, as Clemens points out, one really should avoid special characters when computing things.

1 Like

For what it is worth, \xe9 is unicode representation of é.

I connected those dots, alright.
:slight_smile:

Hi Anders,

here’s an axemple :

SQLite accents drama.py (750 Bytes)
The .db file is HERE (can’t attach)

…and this is what I get :

The reason that you get the weird looking strings is that you’re printing the result tuples instead of the string you want.

The hint is already in your own printing code where you do:

for item in items:
    print item[0] ## << see, you're getting the first item of the result tuple

Fixed version:

# -*- coding: utf-8 -*-

__author__ = "osuir, and small correction by jesterKing"
__version__ = "2021.01.05"

import rhinoscriptsyntax as rs
import Rhino as rh
import sqlite3 as sq
import os

db_file_path = r'C:\Users\Nathan\Downloads\Test accents.db'

conn = sq.connect(db_file_path)

# Create a cursor
c = conn.cursor()

# Query the database and return all records with a sub-routine
c.execute

# Get the strings only
items = [i[0] for i in c.fetchall()]

a = items("""SELECT bogus_field
FROM BOGUS_TABLE""")

# Close the connection
conn.close()

3 Likes

I don’t understand how this can work since you didn’t type the SQL query…
Time for me to go to Python school I guess…

Thanks Nathan.

How the query is done doesn’t really matter. It matters how you handle the results. Passing on a tuple instead of the string you intended is what your original problem was.

I’m going to guess that your test database is just one column with all the records, and the current code just executes essentially a SELECT *;

I know, but this looks like black magic nonetheless !

Do not despair, you’ll get there eventually!

You mean… sending queries by telepathy ?

Side question : how come I don’t see dozens of threads about SQLigth in this forum ?
It seems so fitting to use the power of databases in GH… but no one seems interested.

There are some questions about it every now and then. There’s not much more to say than what one can glean from great coding resources like stackoverflow.com and such. There’s loads of info on SQLite out there :slight_smile:

Meh… it’s really thin. I think it should be promoted somehow.

There’s the Slingshot plugin for mySQL, but there’s even less posts about that one !

Really, GH and SQLite seem like a marriage made in heaven ; maybe it would take someone to create a nice plugin and thus make it more accessible.

What would you envision the plug-in did for you that you can’t do by just typing in the code in your GH Python component?

Finding out the schema of a database and create outputs, maybe even components, based on that automatically?

@osuire, I already see what the confusion here is. I must have made a copy/paste error while copying the new code to Discourse, probably fatfingered the wrong buttons, thus accidently removing the actual query. I’ve fixed the reply with the correct Python code.

Nathan, one of the reasons so many people enjoy GH is that they like to program visually, and do without the hassle of syntax script stickling.
See here, for example, I’m still struggling because for some reason, my version of the script now refuses to output a single thing.

If I had a component with a “Database path” input, a few “SQL query” inputs and the corresponding outputs, I’d be fine. I could focus on getting my job done instead of wondering about utf8, if I need one " or three, if my indentation is good, if I forgot (god forbid !) to type a “:” somewhere, etc…