Writing output of function to a csv file
MRAB
python at mrabarnett.plus.com
Mon May 25 14:25:08 EDT 2020
On 2020-05-25 19:00, CiarĂ¡n Hudson wrote:
> Hi,
>
> In the code below, which is an exercise I'm doing in class inheritance, almost everything is working except outputting my results, which as the function stock_count, to a csv.
> stock_count is working in the code.
> And I'm able to open and close the csv called cars, but the stock_count output is not being written to the file.
> Any suggestions?
>
[snip]
>
> def stock_count(self):
> print('petrol cars in stock ' + str(len(self.petrol_cars)))
> print('electric cars in stock ' + str(len(self.electric_cars)))
> print('diesel cars in stock ' + str(len(self.diesel_cars)))
> print('hybrid cars in stock ' + str(len(self.hybrid_cars)))
>
>
> def process_rental(self):
> answer = input('would you like to rent a car? y/n')
> if answer == 'y':
> self.stock_count()
> answer = input('what type would you like? p/e/d/h')
> amount = int(input('how many would you like?'))
> if answer == 'p':
> self.rent(self.petrol_cars, amount)
> if answer == 'd':
> self.rent(self.diesel_cars, amount)
> if answer == 'h':
> self.rent(self.hybrid_cars, amount)
> else:
> self.rent(self.electric_cars, amount)
> self.stock_count()
>
> file = open("cars.csv","w")
> file.write(str(self.stock_count()))
> file.close()
>
In 'stock_count' you're telling it to print to the screen. Nowhere in
that function are you telling it to write to a file.
More information about the Python-list
mailing list