找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 729|回复: 8

[求助] TypeError: object of type 'int' has no len()问题

1

主题

5

帖子

5

积分

贫民

积分
5
milandudu 发表于 2018-3-22 22:41:07 | 显示全部楼层 |阅读模式
学习《python编程:从入门到实践》16章,代码如下。这是要画一个某股票的收盘价的日均值、月均值的图。但是显示TypeError: object of type 'int' has no len()。我猜测是y_list的问题,但是我用print将它打印,是一个列表不是int类型。究竟哪里出错了呢?
import json
import pygal
import math
from itertools import groupby


# 将数据加载到一个列表中
filename = 'btc_close_2017.json'
with open(filename) as f:
    btc_data = json.load(f)

# 打印每一天的信息
for btc_dict in btc_data:
    date = btc_dict['date']
    month = int(btc_dict['month'])
    week = int(btc_dict['week'])
    weekday = btc_dict['weekday']
    close = int(float(btc_dict['close']))
    print("{} is month {} week {}, {}, the close price is {} RMB".format(date,month,week,weekday,close))

# 创建5个列表,分别存储日期和收盘价
dates = []
months = []
weeks = []
weekdays = []
close = []
# 每一天的信息
for btc_dict in btc_data:
    dates.append(btc_dict['date'])
    months.append(int(btc_dict['month']))
    weeks.append(int(btc_dict['week']))
    weekdays.append(btc_dict['weekday'])
    close.append(int(float(btc_dict['close'])))

line_chart = pygal.Line(x_label_rotation=20, show_minor_x_labels=False)
line_chart.title = '收盘价对数变换(¥)'
line_chart.x_labels = dates
N = 20 # X轴坐标每隔20天显示一次
line_chart.x_labels_major = dates[::N]
close_log = [math.log10(_) for _ in close]
line_chart.add('log收盘价', close_log)
line_chart.render_to_file('收盘价对数变换折线图(¥).svg')


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]):  # 2
        y_list = [v for _, v in y]
        xy_map.append([x, sum(y_list) / len(y_list)])  # 3
    x_unique, y_mean = [*zip(*xy_map)]  # 4
    line_chart = pygal.Line()
    line_chart.title = title
    line_chart.x_labels = x_unique
    line_chart.add(y_legend, y_mean)
    line_chart.render_to_file(title + '.svg')
    return line_chart

idx_month = dates.index('2017-12-01')
line_chart_month = draw_line(months[:idx_month], close[:idx_month], '收盘价月日均值(¥)', '月日均值')
line_chart_month
回复

使用道具 举报

0

主题

20

帖子

20

积分

贫民

积分
20
胡扶林 发表于 2018-3-23 16:35:47 | 显示全部楼层
我觉得你应该把报错信息打印出来我们看一下
回复 支持 反对

使用道具 举报

1

主题

5

帖子

5

积分

贫民

积分
5
milandudu  楼主| 发表于 2018-3-23 20:35:15 | 显示全部楼层
胡扶林 发表于 2018-3-23 16:35
我觉得你应该把报错信息打印出来我们看一下

Traceback (most recent call last):
  File "E:/Python/visuable date/btc_close_2017.py", line 59, in <module>
    line_chart_month = draw_line(months[:idx_month], close[:idx_month], '收盘价月日均值(¥)', '月日均值')
  File "E:/Python/visuable date/btc_close_2017.py", line 55, in draw_line
    line_chart.render_to_file(title + '.svg')
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\ghost.py", line 165, in render_to_file
    f.write(self.render(is_unicode=True, **kwargs))
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\ghost.py", line 122, in render
    .make_instance(overrides=kwargs)
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\ghost.py", line 116, in make_instance
    self.xml_filters)
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\line.py", line 33, in __init__
    super(Line, self).__init__(*args, **kwargs)
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\base.py", line 74, in __init__
    self._draw()
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\base.py", line 291, in _draw
    self._compute_margin()
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\base.py", line 128, in _compute_margin
    self.label_font_size)
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\util.py", line 229, in get_texts_box
    max_len = max(map(len, get_text_title(texts)))
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\util.py", line 224, in get_text_title
    for text in texts:
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\base.py", line 126, in <lambda>
    map(lambda x: truncate(x, self.truncate_label or 25),
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\util.py", line 271, in truncate
    if len(string) > index and index > 0:
TypeError: object of type 'int' has no len()

Process finished with exit code 1
回复 支持 反对

使用道具 举报

1

主题

5

帖子

5

积分

贫民

积分
5
milandudu  楼主| 发表于 2018-3-23 20:35:49 | 显示全部楼层
胡扶林 发表于 2018-3-23 16:35
我觉得你应该把报错信息打印出来我们看一下

已发错误信息,请指点。感谢感谢!!
回复 支持 反对

使用道具 举报

1

主题

5

帖子

5

积分

贫民

积分
5
milandudu  楼主| 发表于 2018-3-23 20:36:14 | 显示全部楼层
milandudu 发表于 2018-3-23 20:35
已发错误信息,请指点。感谢感谢!!

Traceback (most recent call last):
  File "E:/Python/visuable date/btc_close_2017.py", line 59, in <module>
    line_chart_month = draw_line(months[:idx_month], close[:idx_month], '收盘价月日均值(¥)', '月日均值')
  File "E:/Python/visuable date/btc_close_2017.py", line 55, in draw_line
    line_chart.render_to_file(title + '.svg')
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\ghost.py", line 165, in render_to_file
    f.write(self.render(is_unicode=True, **kwargs))
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\ghost.py", line 122, in render
    .make_instance(overrides=kwargs)
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\ghost.py", line 116, in make_instance
    self.xml_filters)
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\line.py", line 33, in __init__
    super(Line, self).__init__(*args, **kwargs)
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\base.py", line 74, in __init__
    self._draw()
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\base.py", line 291, in _draw
    self._compute_margin()
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\base.py", line 128, in _compute_margin
    self.label_font_size)
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\util.py", line 229, in get_texts_box
    max_len = max(map(len, get_text_title(texts)))
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\util.py", line 224, in get_text_title
    for text in texts:
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\graph\base.py", line 126, in <lambda>
    map(lambda x: truncate(x, self.truncate_label or 25),
  File "C:\Users\Fyw\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pygal\util.py", line 271, in truncate
    if len(string) > index and index > 0:
TypeError: object of type 'int' has no len()

Process finished with exit code 1
回复 支持 反对

使用道具 举报

2

主题

10

帖子

10

积分

贫民

积分
10
idontwan 发表于 2018-3-25 13:33:10 | 显示全部楼层
提示的错误出现再哪一行?
回复 支持 反对

使用道具 举报

1

主题

5

帖子

5

积分

贫民

积分
5
milandudu  楼主| 发表于 2018-4-1 15:33:57 | 显示全部楼层
idontwan 发表于 2018-3-25 13:33
提示的错误出现再哪一行?

在这一行        
xy_map.append([x, sum(y_list) / len(y_list)])  # 3
提示TypeError: object of type 'int' has no len()
回复 支持 反对

使用道具 举报

0

主题

1

帖子

1

积分

贫民

积分
1
asd123456 发表于 2018-6-6 10:39:53 | 显示全部楼层
问题解决了吗,老铁,我也遇到相同的问题了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表