首页 > 解决方案 > 删除用于绘制 matplotlib 图的撇号

问题描述

我正在尝试在 matplotlib 上绘制图表,但由于返回的值带有撇号,因此无法正常工作,这是我的代码,

import matplotlib.pyplot as plt
emp_data_list=[]

def read_file():
infile = open ('emp_data.txt', 'r')

for row in infile:
    if not row.startswith('#'):
        row = row.rstrip('\n').split(', ')
        emp_data_list.append(row)
infile.close()

read_file()


for item in range(len(emp_data_list)):
salaries = [stuff[4] for stuff in emp_data_list]
print salaries

我也将其用于薪水:

salaries = [salary for emp_no, name, age, pos, salary, yrs_emp in emp_data_list]

打印薪水时,它会返回:

['29000', '24000', '42000', '21000', '53000', '42000', '50000', '33000', '38000', '22000', '19000', '23000', '44000', '32000', '28000']

我相信这就是为什么我的图表不起作用

标签: pythonmatplotlib

解决方案


尝试在下面的代码中将字符串转换为整数

salaries = [int(salary) for emp_no, name, age, pos, salary, yrs_emp in emp_data_list]

另外,欢迎来到 Stack Overflow!如果它适合您,请将其标记为答案:)


推荐阅读