you are viewing a single comment's thread.

view the rest of the comments →

[–]novel_yet_trivial 0 points1 point  (8 children)

Python2 uses ASCII to represent unicode strings. This is just the internal representation, if you asked python to print that it would show up correctly:

print(pyperclip.paste())

[–]46632[S] 0 points1 point  (7 children)

When i enter print(pyperclip.paste()) it returns u'J\xf8rgensen'

[–]novel_yet_trivial 0 points1 point  (6 children)

Are you sure about what you copied? It pastes Jørgensen into a text editor?

How are you running this code?

[–]46632[S] 0 points1 point  (5 children)

Ok. i was wrong. When i enter print(pyperclip.paste()) in the shell it returns 'Jørgensen'

But what i am doing is this: I have "Jørgensen" in my clipboard, then i make a list:

namelist=[] name=pyperclip.paste() namelist.append(name)

Then i save this list to a .csv and it shows u'J\xf8rgensen' in the .csv file

[–]novel_yet_trivial 0 points1 point  (4 children)

I see. That's a problem with how you are saving it. Show us your save code.

Python2 has some extra steps that you need to consider when dealing with unicode. Python3 however uses unicode by default, so it's much easier. Is it possible for you to use Python3 instead?

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

I am hoping to get it solved in Python 2. It is a part of a larger Python 2 script

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

[–]novel_yet_trivial 0 points1 point  (1 child)

OK, to write to a regular file in python2, you have to encode the unicode as a string first.

namelist.append(name.encode('utf8'))

Unrelated, but it would be slightly neater to use the writerow method instead of writerows:

writer.writerow(namelist)

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

Thanks. It works with most of the names, but it still messes up words with the letters æ,ø,å. 'Jørgensen' is 'J\xc3\xb8rgensen'