I’m new to python and i have been tasked to make a vertical bar chart from a text document, i have put the x axis information into a list and the data into a list as well. The problem i am having is when trying to output the chart I get an error saying : TypeError: unsupported operand type(s) for -: ‘str’ and ‘int’. And I am struggling to figure out how to solve that error.
Do I need to type-cast one of the lists? Or are the lists the wrong way to do it?
Here’s my code:
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart
ExifOutput = open('ExifOutput.txt', 'r')
ImageMakeContainer = []
Container = []
makeList = []
numberList = []
lineReader = ExifOutput.readlines()
for line in lineReader:
if 'Image Make: ' in line:
ImageMakeContainer.append(line[13:-2])
for item in set(ImageMakeContainer):
Container.append(item + " " + str(ImageMakeContainer.count(item)))
print Container
for data in Container:
[int(num) for num in data.split() if num.isdigit()]
make = data[0:-(len(num) + 1)]
makeList.append(make)
numberList.append(num)
print numberList
print makeList
ExifOutput.close()
data = numberList
axis = makeList
graph = Drawing(400, 300)
chart = VerticalBarChart()
chart.width = 300
chart.height = 200
chart.x = 30
chart.y = 40
chart.data = data
chart.categoryAxis.categoryNames = axis
chart.valueAxis.valueMin = 0
graph.add(chart)
graph.save(fnRoot='TestGraph', formats=['png', 'pdf'])
Hope I was clear enough for you. (The print statements are just there to check the contents of the lists)
[EDIT] The output of the prints are as follows:
['Apple 1', 'FUJIFILM 5', 'Samsung Techwin 1']
['1', '5', '1']
['Apple', 'FUJIFILM', 'Samsung Techwin']
To put a Python int into a string, you just need to place it between backticks
like_this. For example,You can also accomplish the same sort of thing using formats, like