I\'m writing a game in python. And after each round of the game (yes, the game has multiple rounds) I want to write the data to a CSV file.
this is my code:
Python 3:
with open('data.csv', 'a', newline='') as fp:
for player in self.players:
a = csv.writer(fp, delimiter=',');
data = [[player.name, player.penalty(), player.score()]];
a.writerows(data);
With python 3 there is change in the CSV module you can read here
Python 2.x:
Just change the open()
to binary open('data.csv', 'ab')
You can set control quoting using:
csv.writer(fp, delimiter=',',quoting=csv.QUOTE_MINIMAL)
As from docs your options are:
csv.QUOTE_ALL Instructs writer objects to quote all fields.
csv.QUOTE_MINIMAL Instructs writer objects to only quote those fields which contain special characters such as delimiter, quotechar or any of the characters in lineterminator.
csv.QUOTE_NONNUMERIC Instructs writer objects to quote all non-numeric fields.
Instructs the reader to convert all non-quoted fields to type float.
csv.QUOTE_NONE Instructs writer objects to never quote fields. When the current delimiter occurs in output data it is preceded by the current escapechar character. If escapechar is not set, the writer will raise Error if any characters that require escaping are encountered.