The code below is the timeclienthandler.py code that I created in Visual Studio Code. It works sometimes but it still gives me the error if I continuously run the code. I don't understand how it works off and on.
from time import ctime
from threading import Thread
import random
class TimeClientHandler(Thread):
def __init__(self, client):
Thread.__init__(self)
self.client = client
def run(self):
msgList = ["There are some idiots who always answer 'No' to every question, now tell me. Are you one of them?","There's nothing to fear. Except maybe that weird guy over there.","If I'm driving you crazy just remember to put on your seat belt.","I wondered why the baseball was getting bigger. Then it hit me.","You're Just Jealous Because The Voices Are Talking To Me.","Quickest way to get on your feet...miss a car payment.","Why do psychics ask your name?","I'm not opinionated. I'm just always right.","Sanity is the playground for the unimaginative.","It isn't homework unless it's due tomorrow."]
msg = msgList[random.randint(0,len(msgList))]
msge ="\n"+msg
self.client.send(bytes(ctime() + msge,"ascii"))
self.client.close()
The error you specified is occuring at line 18 in your code
Specifically - bytes(ctime() + msge,"ascii")
This error occured because of encoding you specified "ascii" which only supports the following characters:
['\x00', '\x01', '\x02', '\x03', '\x04', '\x05', '\x06', '\x07', '\x08', '\t', '\n', '\x0b', '\x0c', '\r', '\x0e', '\x0f', '\x10', '\x11', '\x12', '\x13', '\x14', '\x15', '\x16', '\x17', '\x18', '\x19', '\x1a', '\x1b', '\x1c', '\x1d', '\x1e', '\x1f', ' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '#', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\x7f']
but the input you are giving to it is '\u2019' or {’}, which is not in the above list hence resulting in the UnicodeEncodeError ('ascii' codec can't encode character '\u2019' in position 0: ordinal not in range(128))
So the solution to this problem is to change the specified encoding (ascii) to (UTF-8)
Try the code below:
from time import ctime
from threading import Thread
import random
class TimeClientHandler(Thread):
def __init__(self, client):
Thread.__init__(self)
self.client = client
def run(self):
msgList = ["There are some idiots who always answer 'No' to every question, now tell me. Are you one of them?","There's nothing to fear. Except maybe that weird guy over there.","If I'm driving you crazy just remember to put on your seat belt.","I wondered why the baseball was getting bigger. Then it hit me.","You're Just Jealous Because The Voices Are Talking To Me.","Quickest way to get on your feet...miss a car payment.","Why do psychics ask your name?","I'm not opinionated. I'm just always right.","Sanity is the playground for the unimaginative.","It isn't homework unless it's due tomorrow."]
msg = msgList[random.randint(0,len(msgList))]
msge ="\n"+msg
self.client.send(bytes(ctime() + msge,"UTF-8"))
self.client.close()
Encoding with UTF-8.
Turn to the folder Lib\site-packages in current used python interpreter, create a file called sitecustomize.py and add the following code in it:
#coding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
Then restart the VS Code to see if the question goes away.
I need to read the following data out of a text file;
[L02]
g,g,g,g,g,g,g,g,g,g,w,w,w,w,g,g
g,g,g,g,g,g,g,g,g,w,w,w,w,w,g,g
g,g,g,g,g,g,g,g,w,w,w,w,w,g,g,g
g,g,g,g,g,g,g,g,w,w,w,w,g,g,g,g
g,g,g,g,g,g,g,g,g,w,w,w,w,g,g,g
g,g,g,g,g,g,g,g,g,g,w,w,w,w,g,g
g,g,g,g,g,g,g,g,g,g,g,w,w,w,g,g
g,g,g,g,g,g,g,g,g,g,g,w,w,g,g,g
g,g,g,g,g,g,g,g,g,g,g,w,w,g,g,g
g,g,g,g,g,g,g,g,g,g,w,w,w,g,g,g
g,g,g,g,g,g,g,g,g,w,w,w,g,g,g,g
g,g,g,g,g,g,g,g,w,w,w,w,g,g,g,g
g,g,g,g,g,g,g,w,w,w,w,g,g,g,g,g
g,g,g,g,g,g,g,w,w,w,g,g,g,g,g,g
g,g,g,g,g,g,w,w,w,w,w,g,g,g,g,g
g,g,g,g,g,g,g,w,w,w,w,g,g,g,g,g
[L01]
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d
I can read a single block as a csv file but I don't know how to read each file as a separate list
The output I want is to have arrays/lists for each block with the block contents as the list elements. Any ideas?
Here's a script that demonstrates how to break down the problem into reusable steps (functions) and performs the transformation your need.
import itertools
import operator
import re
import csv
import pprint
class TaggedLine(str):
"""
Override str to allow a tag to be added.
"""
def __new__(cls, val, tag):
return str.__new__(cls, val)
def __init__(self, val, tag):
super(TaggedLine, self).__init__(val)
self.tag = tag
def sections(stream):
"""
Tag each line of the stream with its [section] (or None)
"""
section_pattern = re.compile('\[(.*)\]')
section = None
for line in stream:
matcher = section_pattern.match(line)
if matcher:
section = matcher.group(1)
continue
yield TaggedLine(line, section)
def splitter(stream):
"""
Group each stream into sections
"""
return itertools.groupby(sections(stream), operator.attrgetter('tag'))
def parsed_sections(stream):
for section, lines in splitter(stream):
yield section, list(csv.reader(lines))
if __name__ == '__main__':
with open('data.csv') as stream:
for section, data in parsed_sections(stream):
print 'section', section
pprint.pprint(data[:2])
Save your file as 'data.csv' and the script will run on your data with this output:
section L02
[['g',
'g',
'g',
'g',
'g',
'g',
'g',
'g',
'g',
'g',
'w',
'w',
'w',
'w',
'g',
'g'],
['g',
'g',
'g',
'g',
'g',
'g',
'g',
'g',
'g',
'w',
'w',
'w',
'w',
'w',
'g',
'g']]
section L01
[['d',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd'],
['d',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd',
'd']]
If you have numpy, you could read the file into a numpy array. comments='[' tells np.genfromtxt to ignore lines that begin with [. The reshape method places each 16x16 block in its own "layer".
import numpy as np
arr=np.genfromtxt('data.csv',comments='[',delimiter=',',dtype=None)
arr=arr.reshape(-1,16,16)
You can access the nth layer with arr[n].