python - 删除用于绘制 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']
我相信这就是为什么我的图表不起作用
解决方案
尝试在下面的代码中将字符串转换为整数
salaries = [int(salary) for emp_no, name, age, pos, salary, yrs_emp in emp_data_list]
另外,欢迎来到 Stack Overflow!如果它适合您,请将其标记为答案:)
推荐阅读
- python-3.x - 如何将默认烧瓶记录器输出更改为 json 记录处理程序
- flutter - 在 Flutter 中按下/离开时,TextField 会重新加载 FutureBuilder
- haskell - Liquid Haskell 中函数“map”的正确合约是什么?
- mouseevent - How do i prevent mouse and touch events from trigger for DisplayObjects below a backdrop
- c - 使用外部控制格式字符串的漏洞
- angular - Angular 7 文件 '../src/types/@angular/common.d.ts' 不是模块
- javascript - 查找所有重复的 id 并添加唯一键
- sql-server - 使用触发器替换公式中的值
- c# - CORS 实现显示不允许用于 POST 请求的方法
- android - 如何以编程方式将颜色应用于应用程序主题?