Handling Unicode Encoding Errors in Python 2.7 When Generating Plotly Time‑Series Charts
The author describes encountering a Python 2.7 UnicodeEncodeError when Plotly attempts to open a browser for each generated time‑series chart, explains why the error occurs with non‑ASCII filenames, and shows how adding exception handling resolves the issue while processing weather data for thousands of cities.
The author used Groovy to scrape historical weather data for over 3,000 Chinese cities and then attempted to plot each city's temperature range as a time‑series chart using Plotly. Plotly automatically opens a system browser for each chart, which caused the computer to freeze when generating many charts.
When using Chinese characters as part of the output file name, Python 2.7 raised the following error:
'ascii' codec can't encode characters in position 69-70: ordinal not in range(128)The program stopped, but the file had already been created. By adding appropriate exception handling, the author was able to let the script continue and meet the requirements, despite limited knowledge of Python 2.7 encoding issues.
Below is the relevant Python code (kept intact):
#!/usr/bin/python
# coding=utf-8
from first.date import DatePlot
import os
from second.MysqlFission import MysqlFission
import shutil
import time
class Fission:
x = []
y = []
z = []
d = []
def __init__(self):
print "欢迎使用fission类!"
def getData(self, name):
size = 0;
with open("/Users/Vicky/Documents/workspace/source_api/long/" + name + ".log") as apidata:
for i in apidata:
data = i.split("
")[0].split("|")[0]
low = i.split("
")[0].split("|")[1]
high = i.split("
")[0].split("|")[2]
diff = int(high) - int(low)
self.x.append(data)
self.y.append(low)
self.z.append(high)
self.d.append(diff)
size += 1;
def getDataMarkLine(self, name):
with open("/Users/Vicky/Documents/workspace/source_api/long/" + name + ".log") as apidata:
for i in apidata:
data = i.split("
")[0].split("|")
day = data[0]
time = float(data[1])
self.x.append(day)
self.y.append(time)
return [self.x, self.y]
if __name__ == "__main__":
names = []
for name in names:
name = u"三沙"
sql = MysqlFission()
sql.getWeather(name)
fission = Fission()
fission.x = []
fission.y = []
fission.z = []
fission.d = []
fission.getData(name)
try:
DatePlot.MakePlotTwo(fission.x, name, high=fission.y, low=fission.z, diff=fission.d)
except BaseException:
print 2
shutil.copyfile(name + ".html", "/Users/Vicky/Desktop/w/" + name + ".html")
os.remove(name + ".html")
time.sleep(5)After fixing the encoding issue, the script successfully generates the time‑series plots, as illustrated by the example chart for Beijing shown in the article.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
