0

Hello I'm VERY new in python. I just have to do 1 thing with it. When i print my string names, this is what comes up:

{'id': 1, 'xd_id': 2, 'name': 'nameea', 'description': 'somethingveryweird', 'again_id': 6, 'some_id': None, 'everything': False, 'is_ready': False, 'test_on': None, 'something': None, 'something': [], 'count_count': 28, 'other_count': 0, 'again_count': 0, 'new_count': 0, 'why_count': 0, 'custom_count': 0, 'custom2_count': 0, 'custom3_count': 0, 'custom4_count': 0, 'custom5_count': 0, 'custom_status6_count': 0, 'custom7_count': 0, 'lol_id': 7, 'wtf_id': None, 'numbers_on': 643346, 'something_by': 99, 'site': 'google.com'}

I would to get this info to excel with the left row being the "id": and the right being the 1. And all the info like this. for example. "site" on the left and "google.com" on the right. my current code adds all this info to the first row on the excel and i can't seem to find any tutorial for this. Thanks for all answers. My current code:

   f = open('test.csv', 'w')
   s = str(names)
   f.write(s)
   f.close()
  • See the [csv](https://docs.python.org/3/library/csv.html) module documentation. – glibdud Jun 29 '17 at 12:18
  • It looks like `names` is a dictionary. You should learn more about this datatype. Don't convert it to a string, use the methods of the dictionary instead. – Matthias Jun 29 '17 at 12:35
  • Possible duplicate of [Python Dictionary to CSV](https://stackoverflow.com/questions/8331469/python-dictionary-to-csv) – JeffUK Jun 29 '17 at 12:38

1 Answers1

1

if python is not going to be your key skill and only this task needs to be done, then here is the answer.

f = open('test.csv', 'w')
csvwt = csv.writer(f)
for x in names.items():
  csvwt.writerow(x)
f.close()

if you want to write to an excel, then you have to do this,

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
row = 0
col = 0
for x in names.items():
    worksheet.write(row, col,     str(x[0]))
    worksheet.write(row, col + 1, str(x[1]))
    row += 1
workbook.close()
Swagat
  • 617
  • 5
  • 16
  • Traceback (most recent call last): File "C:\Users\Eemil\Desktop\name.py", line 50, in f.writerow(x) AttributeError: '_io.TextIOWrapper' object has no attribute 'writerow' –  Jun 30 '17 at 05:20
  • 1
    thank you so much it worked. kind of. It did what it supposed to do but there was an empty line between each one (don't really mind about that) but they were like id,1 on the first row and xd_id,2 on the second. I quess i didn't explain well enought, my bad but i wanted for the "id" to be for example on the Line A and the number on the Line B. So the A1 would be id and B1 would be 1 –  Jun 30 '17 at 07:22
  • didn't follow you completely, this is what csv (comma separated values) is for. If you open csv file in excel, you can find the id in A1 and value in B1. If you want to write it in excel file you need to use http://xlsxwriter.readthedocs.io/ package, which is a long task for you – Swagat Jun 30 '17 at 07:34
  • BTW, in excel line(row) are denoted as number and column are in alphabet – Swagat Jun 30 '17 at 07:37
  • Ok. i actually tried the the xlswriter and got code for it but then i don't know why but it said 'str' object has no attribute 'items'. So i quess i try to stick with your code or start different post to look for answer for the new code. first_row=0 for header in ordered_list: col=ordered_list.index(header) ws.write(first_row,col,header) row=1 for name in name: for _key,_value in name.items(): col=ordered_list.index(_key) ws.write(row,col,_value) row+=1 wb.close() –  Jun 30 '17 at 07:45
  • not able to read your code in comment, try to paste it in your question – Swagat Jun 30 '17 at 07:54
  • your code looks good and i think it might work. Can you tell what i should import from xlswriter? I get : NameError: name 'xlsxwriter' is not defined but i only have Workbook imported from xlsxwriter –  Jun 30 '17 at 08:04
  • No, `import xlsxwriter` only – Swagat Jun 30 '17 at 08:33
  • it is better to use `with` (explanation [here](https://www.python.org/dev/peps/pep-0343/), [here](https://stackoverflow.com/q/3012488/1562285) or [here](https://stackoverflow.com/q/1369526/1562285) than `open` and `close` – Maarten Fabré Jun 30 '17 at 08:41
  • well.. now got even more errors XDD Wrote an answer for easier reading Traceback (most recent call last): worksheet.write(row, col + 1, x[1]) , worksheet.py", line 65, in cell_wrapper return method(self, *args, **kwargs) worksheet.py", line 433, in write raise TypeError("Unsupported type %s in write()" % type(token)) TypeError: Unsupported type in write() –  Jun 30 '17 at 08:41
  • There are some list in the value, convert into string will solve the problem, check my update – Swagat Jun 30 '17 at 08:49
  • 1
    that works. THANK YOU SO MUCH! you are a king. Going to bookmark you and try to help you if you ever get in trouble with C++/C# or php. (those languages i actually know and have been using for years) –  Jun 30 '17 at 08:53
  • Thanks for kind words :) – Swagat Jun 30 '17 at 09:12
  • 1
    You can make it simpler by using XlsxWriter's `write_row()` method (note the underscore, which is a different spelling than the `csv` module's `writerow()`.). – John Y Jun 30 '17 at 22:31