Home > other >  Solution: python error TypeError: TypeError: the object of type "int" has no len () how to
Solution: python error TypeError: TypeError: the object of type "int" has no len () how to

Time:10-13

CodePudding user response:

In this line of code: line_chart x_labels=x_unique
Is your x_unique plastic tuple
The plastic tuples into type String list
Then assigned to line_chart. X_labels was no problem ~

CodePudding user response:

Because of you in the previous month and close into int, prompt int has no len, if not in front of the transformation, there is no problem

CodePudding user response:

I am also a beginner, here is my address code, defines two functions, a data, a drawing, the original poster of the mistakes, mainly because of drawing in the abscissa must be a string, the solution for the following
GBK # coding=
The import pygal
The import math
The import json
The from itertools import groupby



Def get_data (filename, dates, have, weekes, weekdays, close) :

With the open (filename) as f:
btc_data=https://bbs.csdn.net/topics/json.load (f)
For btc_dict btc_data in:
Dates. Append (btc_dict [' date '])
Have append (int (btc_dict [' month ']))
Weekes. Append (int (btc_dict [' week ']))
Weekdays. Append (btc_dict [' weekday '])
Close. Append (int (float (btc_dict [' close '])))

Def draw_line (x_data y_data, title, y_legend) :
Xy_map=[]

For x, y in groupby (sorted (zip (x_data y_data)), key=lambda _ : _ [0]) :
Y_list=y] in [v for _, v
Xy_map. Append ([x, the sum (y_list)/len (y_list)])

X_unique, y_mean=[* zip (* xy_map)]

Line_chart=pygal. Line ()
Line_chart. Title=title

"' abscissa # note that must be a string, using a digital trigger TypeError: TypeError: the object of type
# 'int' has no len () the abnormal "'
New_x_unique=[]
For x in x_unique:
New_x_unique. Append (STR (x))
Print (new_x_unique)
Line_chart. X_labels=new_x_unique

Line_chart. Add (y_legend y_mean)
Line_chart. Render_to_file (title + 'SVG')
Return line_chart


Dates, have, weekes, weekdays, close=[], [], [], [], []
Get_data (' btc_close_2017. Json, dates, have, weekes, weekdays, close)

Idx_month=dates. The index (' 2017-12-01 ')
Line_chart_month=draw_line (up [: idx_month], close [: idx_month], 'average daily closing price on', 'average daily price)
line_chart_month

CodePudding user response:


This is my run results

CodePudding user response:

Thank you, this problem for two days, and finally solved the

CodePudding user response:

And thanks for everyone, for half of the afternoon, finally solved the

CodePudding user response:

Thank you very much!!!!!! I put all function one by one, haven't found the problem, the original is a type of problem,

CodePudding user response:

Thought such broad mistakes can't search the answer on the Internet, search results will search here, thank you all for the great god

CodePudding user response:

reference wuxiajuan126 reply: 3/f
I am also a beginner, here is my address code, defines two functions, a data, a drawing, the original poster of the mistakes, mainly because of abscissa in drawing must be a string, the following solutions
GBK # coding=
The import pygal
The import math
The import json
The from itertools import groupby



Def get_data (filename, dates, have, weekes, weekdays, close) :

With the open (filename) as f:
btc_data=https://bbs.csdn.net/topics/json.load (f)
For btc_dict btc_data in:
Dates. Append (btc_dict [' date '])
Have append (int (btc_dict [' month ']))
Weekes. Append (int (btc_dict [' week ']))
Weekdays. Append (btc_dict [' weekday '])
Close. Append (int (float (btc_dict [' close '])))

Def draw_line (x_data y_data, title, y_legend) :
Xy_map=[]

For x, y in groupby (sorted (zip (x_data y_data)), key=lambda _ : _ [0]) :
Y_list=y] in [v for _, v
Xy_map. Append ([x, the sum (y_list)/len (y_list)])

X_unique, y_mean=[* zip (* xy_map)]

Line_chart=pygal. Line ()
Line_chart. Title=title

"' abscissa # note that must be a string, using a digital trigger TypeError: TypeError: the object of type
# 'int' has no len () the abnormal "'
New_x_unique=[]
For x in x_unique:
New_x_unique. Append (STR (x))
Print (new_x_unique)
Line_chart. X_labels=new_x_unique

Line_chart. Add (y_legend y_mean)
Line_chart. Render_to_file (title + 'SVG')
Return line_chart


Dates, have, weekes, weekdays, close=[], [], [], [], []
Get_data (' btc_close_2017. Json, dates, have, weekes, weekdays, close)

Idx_month=dates. The index (' 2017-12-01 ')
Line_chart_month=draw_line (up [: idx_month], close [: idx_month], 'average daily closing price on', 'average daily price)
Line_chart_month

Originally there are so many friend in this book, can find the same problem, very helpful, thank you very much!

CodePudding user response:

According to the above the second floor laoge method to change the source code
Line_chart. X_labels=x_unique

-> Line_chart. X_labels=[STR (_) for _ in x_unique]

This compilation type errors will not happen again
  • Related