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()
3
  • See the csv module documentation. Commented Jun 29, 2017 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. Commented Jun 29, 2017 at 12:35
  • Possible duplicate of Python Dictionary to CSV Commented Jun 29, 2017 at 12:38

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

14 Comments

Traceback (most recent call last): File "C:\Users\Eemil\Desktop\name.py", line 50, in <module> f.writerow(x) AttributeError: '_io.TextIOWrapper' object has no attribute 'writerow'
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
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 xlsxwriter.readthedocs.io package, which is a long task for you
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)
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().).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.