Mar-03-2022, 07:36 PM
I'm trying to write the dictionary straightlist in a csv file like this
Rank , Hand
1,[['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X'], ['14', 'X']]
2,[['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X']]
3,[['8', 'X'], ['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X']]
...
Rank , Hand
1,[['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X'], ['14', 'X']]
2,[['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X']]
3,[['8', 'X'], ['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X']]
...
import csv filename = 'rankedhands.csv' fields = ['Rank','Hands'] straightlist = { 1: [['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X'], ['14', 'X']], 2: [['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X'], ['13', 'X']], 3: [['8', 'X'], ['9', 'X'], ['10', 'X'], ['11', 'X'], ['12', 'X']], 4: [['7', 'X'], ['8', 'X'], ['9', 'X'], ['10', 'X'], ['11', 'X']], 5: [['6', 'X'], ['7', 'X'], ['8', 'X'], ['9', 'X'], ['10', 'X']], 6: [['5', 'X'], ['6', 'X'], ['7', 'X'], ['8', 'X'], ['9', 'X']], 7: [['4', 'X'], ['5', 'X'], ['6', 'X'], ['7', 'X'], ['8', 'X']], 8: [['3', 'X'], ['4', 'X'], ['5', 'X'], ['6', 'X'], ['7', 'X']], 9: [['2', 'X'], ['3', 'X'], ['4', 'X'], ['5', 'X'], ['6', 'X']] } with open(filename, 'w') as csvfile: csvwriter = csv.DictWriter(csvfile, fieldnames = fields) csvwriter.writeheader() csvwriter.writerows(straightlist)but i get this error:
Error:Traceback (most recent call last):
File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
exec(code, self.locals)
File "/home/fook/Documents/pygame/Poker Practice/test.py", line 228, in <module>
csvwriter.writerows(straightlist)
File "/usr/lib/python3.8/csv.py", line 157, in writerows
return self.writer.writerows(map(self._dict_to_list, rowdicts))
File "/usr/lib/python3.8/csv.py", line 147, in _dict_to_list
wrong_fields = rowdict.keys() - self.fieldnames
AttributeError: 'int' object has no attribute 'keys'
can you help me please