Skip to content
Advertisement

Python: how can I change amount_sold and price_per_unit from string to int

I want to change amount_sold and price_per_unit from string to int but its error. After I zipped the data, each attribute becomes a tuple. Then, I try to change to list by using this code (list(amount_sold)) but it resulting in an error

path = 'data.txt'   
f = open(path)        

for line in f:       
pass          

data = [line.rstrip('n').split(' ') for line in open(path)]      
data      


> output
[['2020-09-23', 'Item_A', '5', '1.90'],
['2020-09-23', 'Item_B', '6', '1.20'],
['2020-09-23', 'Item_A', '2', '1.90'],
['2020-09-23', 'Item_B', '3', '1.20'],
['2020-09-24', 'Item_A', '13', '1.90'],
['2020-09-24', 'Item_B', '2', '1.20'],
['2020-09-24', 'Item_C', '8', '1.30'],
['2020-09-25', 'Item_D', '7', '2.70'],
['2020-09-25', 'Item_A', '15', '1.90'],
['2020-09-25', 'Item_D', '7', '2.70'],
['2020-09-25', 'Item_B', '6', '1.20'],
['2020-09-26', 'Item_B', '1', '1.20'],
['2020-09-26', 'Item_C', '8', '1.30'],


date, item_name, amount_sold, price_per_unit = zip(*data)    
int(date)    


>I get an error at this line that is TypeError: 'list' object is not callable

Advertisement

Answer

I’m not sure if I’ve got this right because you don’t explain what line_list is. However, it seems reasonable to assume that line_list == data. Making that assumption, reconstructing your data from the output you give, using your line of code with the zip call, and then doing what you’re asking about in your subject, I don’t have a problem. Your code seems pretty right on to me. Here’s what I did with your code:

data = [['2020-09-23', 'Item_A', '5', '1.90'],
['2020-09-23', 'Item_B', '6', '1.20'],
['2020-09-23', 'Item_A', '2', '1.90'],
['2020-09-23', 'Item_B', '3', '1.20'],
['2020-09-24', 'Item_A', '13', '1.90'],
['2020-09-24', 'Item_B', '2', '1.20'],
['2020-09-24', 'Item_C', '8', '1.30'],
['2020-09-25', 'Item_D', '7', '2.70'],
['2020-09-25', 'Item_A', '15', '1.90'],
['2020-09-25', 'Item_D', '7', '2.70'],
['2020-09-25', 'Item_B', '6', '1.20'],
['2020-09-26', 'Item_B', '1', '1.20'],
['2020-09-26', 'Item_C', '8', '1.30']]

date, item_name, amount_sold, price_per_unit = zip(*data)

print(amount_sold)

x = list(amount_sold)
print(x)

Result:

('5', '6', '2', '3', '13', '2', '8', '7', '15', '7', '6', '1', '8')
['5', '6', '2', '3', '13', '2', '8', '7', '15', '7', '6', '1', '8']

As you can see, I get a tuple for amount_sold just like you say. But I can turn it into a list even though you say you get an error when you do this.

If you can see what you’re doing different than what’s going on here, maybe it will help you to figure out what’s going on. I don’t see by looking at your code, how you would be getting an error like TypeError: 'list' object is not callable. I’m curious why that is coming up for you.

I figure you probably already know this, but you can turn your lists of strings into lists of numbers with an expression like this:

amount_sold = [int(x) for x in amount_sold]
print(amount_sold)

Result:

[5, 6, 2, 3, 13, 2, 8, 7, 15, 7, 6, 1, 8]
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement