I think I'm adding the path to an image folder incorrectly in the logic of my discord.py bot - python-3.5

I am trying to get the bot to respond with a randomly selected image from a folder on my pc:
if message.content == "look at this":
imgList = os.listdir("C:\Users\Alien\Desktop\BOTS\TAL\IMAGES")
imgString = random.choice(imgList)
path = "C:\Users\Alien\Desktop\BOTS\TAL\IMAGES" + imgString
await client.send_file(message.channel, path)
This is part of a longer .py file with a lot of different code that all works fine with the necessary intros/outros etc
It ran fine before I added this but now when I try to run it prints:
C:\Users\Alien\PycharmProjects\tal-1.0\venv\Scripts\python.exe C:/Users/Alien/PycharmProjects/tal-1.0/tal-1.0.py
File "C:/Users/Alien/PycharmProjects/tal-1.0/tal-1.0.py", line 27
imgList = os.listdir("C:\Users\Alien\Desktop\BOTS\TAL\IMAGES")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
Process finished with exit code 1

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
This is telling you there an escape character error, in position 2-3, which are characters \U
\ is an escape character for strings. It allows you to include things like a single-quote inside a single quote string: var = 'you\'re' will keep the single quote without closing the string.
You're using the escape character \ in your string(, which you're doing because it is part of your filesystem path). So it's trying to decode the next character, U, which it doesn't know how to do since it doesn't need to be escaped.
Instead you need to escape the escape character. You'll need to write \\ in each place that you have \.
Your solution needs something like this in all your paths:
imgList = os.listdir("C:\\Users\\Alien\\Desktop\\BOTS\\TAL\\IMAGES")

Related

Converting string to 64 bit floats in node.js

This question has two inter-related parts which I am deeply struggling with. I just want to point out I have literally been struggling with this for days!
Part 1. I have a string of data which is like the following:
?\21167\200Z\251\3072\26467\000\350-\306%\33767\240\314\203\310\030\n77\200U\374\307\013577\000#\036\306\376_77\200s\215\307\361\21277\000t\235\306\344\26577\000\204\000\307\327\34077\000\264\217\306\313\01387\000R&\307\276687\000\312\210\306\261a87\000\364\026\306\244\21487p\257\"\311\227\26787\000#U\306\212\34287\000\324\210\306}\r97\000\274*\306p897\000\270\016\306J\27197\000\250u\306=\34497\000\224r\3060\017:7\000\316\213\306#::7#\254\223\310\026e:7\000d\251\306\374\272:7\000(-\306\360\345:7\000\322\202\306\343\020;7\200D\230\307\311f;7\000\\\314\306\274\221;7\0000\246\306\257\274;7\000\230\342\305\242\347;7\000\000\220\310\225\022<7\0002\003\307\210=<7\200X\316\307|h<7\000-\033\307o\223<7\000\000\367\305b\276<7\000|\237\306U\351<7\000Di\306H\024=7\000\356>\307;?=7\000\024u\306!\225=7\240J\317\310\025\300=7\000\224#\306\010\353=7\000\373\027\307\373\025>7\0008p\310\324\226>7\000\360\205\306\272\354>7\000\226m\307\255\027?7\000\224\304\306\241B?7\200D\240\310\224m?7\000\304x\306\207\230?7\000B\337\306z\303?7\000;\217\307`\031#7\000\252q\307SD#7\000\244d\3069\232#7\000\270\324\306-\305#7\200\311\266\307 \360#7\340P\233\310\023\033A7\000\014\245\307\006FA7\000B\324\307\371pA7\000\362\002\307\354\233A7\000L#\306\337\306A7\000$\031\306\322\361A7\000\002\004\307\306\034B7\000,U\306\254rB7\000\274\341\306\237\235B7 W\212\310\222\310B7\000\177N\307\205\363B7\0004\351\306x\036C7\000\004\037\306kIC7#;f\310^tC7\000\371Q\307E\312C7\000$f\3078\365C7\000\032\320\306+ D7\000\226\252\306\036KD7\200\350\216\310\021vD7\000\270?\306\367\313D7\000#\032\306\353\366D7\200\2100\310\336!E7\000\270[\306\321LE7\000\"{\307\304wE7\000^\301\306\267\242E7\000\246\240\306\252\315E7\000b\006\307\235\370E7\200\003M\311\220#F7\000\374\203\306\203NF7\000T{\306wyF7\000PT\306j\244F7\000\364^\306]\317F7\000\324\003\307P\372F7\000\303 \307K^67\000\204\000\306X367\000\324\341\306e\01067\000\271\001\307r\33557\000\374\004\306\177\26257\000\240\235\305\214\20757\000\217\242\307\231\\57\000PS\306\246157 \013\236\310\262\00657\000 \222\307\277\33347\000\244\210\306\314\26047\000\226\353\306\346Z47\200\257P\310\331\20547\000\213M\307\363/47\000$\021\306\000\00547\000\373\241\307\r\33237\000z\355\307\032\25737\000\010\244\306&\20437\000\271p\3103Y37\000\307f\307#.37\000\241^\307M\00337\000l&\306Z\33027\000\226\213\306g\25527\000fx\307t\20227\300\352\020\310\201W27\000\253\231\307\215,27\000v\267\306\247\32617#\373r\310\264\25317\000X\202\305\301\20017\300\357\020\310\316U17\000\016\227\306\333*17\000\030\010\307\350\37707\300XN\310\365\32407\000\350U\306\001\25207\000\344\363\306\016\17707\200\324\360\307\033T07\000\022\"\307()07 \213\375\3105\376/7\000\265\222\307O\250/7\000\251r\307B\323/7\200O\215\307\\}/7\200\235\376\307iR/7\000\324\274\307\202\374.7\000S*\307u\'/7\000R-\307\217\321.7\000j\177\307\234\246.7\200)\346\307\251{.7\000\364\350\307\266P.7\000!\310\307\303%.7\000-/\307\320\372-7\200\'\207\307\334\317-7\300\357\006\310\351\244-7\000$(\310\366y-7\000\177:\307\003O-7\200t\361\307\020$-7\0001\217\307\035\371,7\000`G\305*\316,7\200\t3\3107\243,7\300\022\017\310Dx,7\000\244m\307PM,7\000\327o\307]\",7\000\004s\307j\367+7 \360\257\310w\314+7\000\265;\307\204\241+7\000D2\306\221v+7\000\261`\307\236K+7\000 \313\306\253 +7 \337!\311\267\365*7#6L\310\304\312*7\000\271;\310\321\237*7\000%\205\307\336t*7\000\0145\307\353I*7\200E\242\307\370\036*7\000~E\307\005\364)7\000\311\031\307\022\311)7\000\302\374\307\037\236)7\200\276\t\310+s)7\000\261Z\3108H)7\200\350\325\307E\035)7\240\321\201\310R\362(7\000\276\334\307_\307(7\000\246\016\310l\234(7\300\006\254\310yq(7\000\266\024\310\206F(7\200Q2\310\223\033(7\200d\235\307\237\360\'7\000\311\314\307\254\305\'7#i\250\310\271\232\'7\300\254Y\310\306o\'7#\260\t\310\323D\'7\000\336\232\307\340\031\'7\000w&\307\355\356&7\360\226\017\311\372\303&7\000\240\226\306\006\231&7\200\253Q\310\023n&7#OT\310 C&7\000\034&\310-\030&7\000\213d\310:\355%7\000\000\200\302\363\202\0317\000\031\273G\000X\0317\000\246\276G\r-\0317\000\314\271F\032\002\0317\000\242\037G\'\327\0307\200\334\237G4\254\0307\3401\260HA\201\0307\000\262\307FNV\0307\000\230_FZ+\0307\000\022\256Fg\000\0307\200\354\232Gt\325\0277\000\246\355F\201\252\0277\000\336\355F\216\177\0277\000:$G\233T\0277\000\324jF\250)\0277\000\273\231G\265\376\0267p\365\031I\302\323\0267\000j\240G\333}\0267\000\370\247E\350R\0267\000tbF\365\'\0267\000Q7G\034\247\0257\000\304\036F)|\0257\000\214mF5Q\0257`\254\203HB&\0257\000\213|GO\373\0247\000o#Hi\245\0247\000\026\213Fvz\0247\000\014\346F\203O\0247\000\277\236G\220$\0247\000\014\214F\235\371\0237\200\355\345G\251\316\0237\000\312\253F\266\243\0237\200\001\203G\303x\0237\200\034\206G\320M\0237\000\030\236G\335\"\0237#x\033H\352\367\0227\000\3630G\367\314\0227\000>\260F\004\242\0227\000|\242F\021w\0227\000\240uH\035L\0227\000Z\005G7\366\0217\000\':GQ\240\0217\000-\tGkJ\0217\000\340LFx\037\0217\000\224\316F\204\364\0207\000\270\365F\221\311\0207\000\004\332F\236\236\0207\000\224\021G\253s\0207\000\334UG\270H\0207\000\274\211F\305\035\0207\200\243\353G\337\307\0177\000z\037H\370q\0177\000\t3G\022\034\0177\000\335<G\037\361\0167#\275+H,\306\0167\200\323\272G9\233\0167\300\030)HSE\0167\000\026\237F_\032\0167\000L\227Fl\357\r7\000~\243Fy\304\r7\000\306\037G\206\231\r7\000\334XF\240C\r7\000 dF\272\355\0147\000\374\324F\307\302\0147\000\014\207F\340l\0147\000\327CG\372\026\0147\000\370\261F\007\354\0137\000\025\003G\024\301\0137# \\H!\226\0137\000\254\247F.k\0137\000\024\270F;#\0137\000\362\216Fa\277\n7\000\020\023F{i\n7\000(\033G\347\255\0317\0008\226E\332\330\0317\000\000\276F\315\003\0327\000\230\253E\300.\0327\000XEF\263Y\0327\000cSH\246\204\0327\200`\224G\231\257\0327\000\366\337G\214\332\0327\000P\353Es0\0337\000&\212Ff[\0337\000\263JGY\206\0337\000\212gG?\334\0337\200\260\202GL\261\0337\000\324PF2\007\0347\340\023\037I%2\0347\000\256\314F\030]\0347\000\250#F\013\210\0347\000<\251G\377\262\0347\000\320hE\362\335\0347\000\270nF\3303\0357\200\263\327G\313^\0357\000\016\227F\276\211\0357\200\313\244G\261\264\0357\0005\262G\244\337\0357\200\307\301G\230\n\0367\2001\200G\2135\0367\000\020:F~`\0367\200\330\234Gq\213\0367\200\222\314Gd\266\0367\000\357BGW\341\0367\000\240\234FJ\014\0377\200\025\332G=7\0377\000d\021F0b\0377\000dhH$\215\0377\000\002iG\027\270\0377\200[\246G\n\343\0377\200e\264G\375\r 7\000 2G\3608 7\000#\302E\343c 7\200y\250G\326\216 7\000\335DG\311\271 7\000\323\263G\275\344 7\000B\214F\260\017!7\200\246\243G\226e!7\0006`G\243:!7\200\036\344G\211\220!7\200u\371G|\273!7\000\340\202Go\346!7\000\272\377Fb\021\"7#v<HU<\"7\000\"\330FIg\"7\000\004FG<\222\"7\200;\271G/\275\"7\200C\243H\"\350\"7\000\370\337F\025\023#7\300\r\001H\010>#7\000\215^H\373h#7\200K\202G\356\223#7\000\236\350G\341\276#7\200k\303G\325\351#7\000F\346G\310\024$7\3002\030H\273?$7\200\220\276G\241\225$7`d\371H\256j$7\240\275\036I\224\300$7#y\036H\207\353$7\000#\237Gz\026%7\000^<Gal%7\200^?HnA%7\2000\231HT\227%7\200\310\323GG\302%7\300+DH
I am reading data from a file and I end up with a variable myString with the above data in it. When I do typeof on it, it says it's a string and when I do a console.log on it, it outputs the above exactly.So I created a new node.js file, and put all of the data above inside quotes:
newString = "?\21167\200Z\251\3072\26467\000 etc"
When I console.log(newString) it outputs
?‰67€Z©Ç2´67è-Æ%ß67 Ìƒ77€UüÇ 577ÉÆþ_77€sÇñŠ77tÆäµ77„Ç×à7 etc
Rather than the original string "?\21167\200Z\251\3072\26467\000..." etc
What is going on here?
Part 2. Because of 1 I have struggled to to extract the 64 bit floats from this string.
I can do it Python!:
import numpy as np
data_new = b"?\21167\200Z\251\3072\26467\000\350-\306%\33767\240\314\2 etc etc"
print(np.frombuffer(data_new, dtype="f4,f4"))
Is there an easy way to do this in node?
Explanation
In JavaScript, \<OCTAL SEQUENCE> inside a string literal will give you the Unicode character represented by that codepoint in octal. This feature is deprecated in modern JavaScript but still works for backward-compatibility reasons (except in strict mode, where it throws a SyntaxError).
The octal sequence must be in the range 0o0..0o377 (decimal 0..255 or hexadecimal 0x0..0xff) for this to happen.
'\1'.codePointAt(0).toString(16) // 1
String.fromCodePoint(0x01) // <Start of Heading>
// https://www.compart.com/en/unicode/U+0001
'\377'.codePointAt(0).toString(16) // ff
String.fromCodePoint(0xff) // "ÿ"
If the number isn't valid Octal, the backslash is simply "swallowed":
'\9'.codePointAt(0).toString(16) // 39
String.fromCodePoint(0x39) // "9"
// 9 is not a valid octal number, so the string
// simply evaluates to the character "9"
Solutions
Option 1
Replace all the backslashes \ with double-backslashes \\ to escape them, then you should be good to go.
'\\1'.codePointAt(0).toString(16) // 5c
String.fromCodePoint(0x5c) // Reverse Solidus (AKA backslash)
// https://www.compart.com/en/unicode/U+005C
Option 2
Use String.raw with a template literal.
String.raw`\1\2\3` === "\\1\\2\\3" // true
Note that this will not work if there is a backslash at the very end of the string:
String.raw`\` // Uncaught SyntaxError: Unexpected end of input

Get rid of invalid unicode character in string variable

I've put in a python3 requests get command (not sure if that's good phrasing), converted it to json, and have parsed it to receive the name:
'Harrison Elementary School \U0001f3eb'
I looked it up and the unicode character stands for a school, Unicode School Character. But when I print it, I get:
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f3eb' in position 27: character maps to <undefined>
I really don't care about having that unicode character. It's not important for my purposes.
How can I strip that unicode character and any other invalid characters from this or any string I come across?
The character is not really invalid, just undefined, so when you are encoding you can often tell the encoder how to handle errors:
import codecs
school_name = "Harrison Elementary School \U0001f3eb"
encoded_name = codecs.charmap_encode(school_name, 'ignore')
print(encoded_name)
With result (b'Harrison Elementary School ', 28)
First, you have to determine why the characters are invalid. It appears that the error message was generated when you tried to print the string, meaning that the Unicode character could not be encoded using the default output encoding. For print this should be sys.stdout.encoding.
You can encode the string yourself and ignore characters that are invalid, but that leaves you with a byte string. It is necessary to decode those bytes back into a Unicode string.
def sanitize(s, encoding, errors='ignore'):
return s.encode(encoding, errors=errors).decode(encoding)
>>> import sys
>>> print(sanitize('Harrison Elementary School \U0001f3eb', sys.stdout.encoding))
Harrison Elementary School

Multiple checks but still SyntaxError: EOL while scanning string literal

I have checked on this string multiple times to ensure that the (".") are in place, but the message
File "<ipython-input-13-ef09f7b4583b>", line 48 plt.savefig("C:\scratch\\data\"+str(angle).zfill(3)+".")
SyntaxError: EOL while scanning string literal
still comes up.
Any suggestions?
if save is not False:
plt.savefig("C:\scratch\\data\"+str(angle).zfill(3)+".png")
plt.close("all")
else:
plt.show()
return
A Python string can not terminate with \ as this will escape the closing " (or ').
You have several options:
Use double-back slashes in a constant manner:
plt.savefig("C:\\scratch\\data\\" + str(angle).zfill(3) + ".png")
Use .format, preferably with combination of raw string to avoid problems in case a directory name starts with t, n or any other character that will become a control sequence when prefixed with \:
plt.savefig(r"C:\scratch\data\{}.png".format(str(angle).zfill(3)))

Why does "\1" inside a triple-quoted string evaluate to a unicode 0x1 code point

I wanted a String containing a text \1.
What I did was (the real string was longer but it's not important):
'''
\1
'''
Which resulted in a String containing a unicode 0x1 codepoint.
I think what I should've done is just escape the backslash like this:
'''
\\1
'''
What I don't understand is why Groovy didn't report an error here. I thought unicode escapes are supposed to look like \u1?
Instead of a syntax error I got a runtime exception when I tried to put this String into an XML element:
An invalid XML character (Unicode: 0x1) was found in the element content of the document.
The \ (backward slash) symbol is an escape symbol. If you mean to use it literally, you must escape it itself: \\.
When you escape any character, the character is interpreted to have special meaning. In the case of the \1 sequence, it just happens that this can be interpreted as the 0x01 codepoint.
This is the same in Java Strings.
If you want to not have to escape characters in Groovy, use slashy strings:
def x = /\1/
assert x == "\\1"
which also works as multiline:
def x = /
\1
/

Use 'r' while reading a file

Stuck in a silly situation.
A variable has a path name and trying to open it .
file_path = 'C:\Users\ARC\Desktop\A3\wordlist1.txt'
f = open(file_path)
This gives error : SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape (, line 1)
Obviously , using an 'r' in front of file name solves this . i.e
f = open(r'C:\Users\ARC\Desktop\A3\wordlist1.txt')
Now , how do i append the 'r' with a variable? Or if there is another way? I am thinking
if an 'execute' statement can be used in some way? I am trying this with Python 3
r tells the Python interpreter that the string is to be interpreted as a raw string (i.e. don't process escapes). This is obviously useful when you have escaped strings hardcoded in your source code, but if you get your path from an external source (e.g. read it from a file), you don't need to bother with r at all.

Resources