title: Converting json to csv keeping non-ascii characters intact
body: <p>I have a .json file which is in this form</p>
  
  <pre><code>{"contributors": null, "truncated": false, "text": "“cool jeans,” i tell a cute boy\nlittle did he know that im talking about his genes bc those chromosomes have combined beautifully ay papi", "is_quote_status": false, "in_reply_to_status_id": null, "id": 786650297116532736, "favorite_count": 631, "source": "&lt;a href=\"http://bufferapp.com\" rel=\"nofollow\"&gt;Buffer&lt;/a&gt;", "retweeted": false, "coordinates": null, "entities": {"symbols": [], "user_mentions": [], "hashtags": [], "urls": []}, "in_reply_to_screen_name": null, "in_reply_to_user_id": null, "retweet_count": 233, "id_str": "786650297116532736", "favorited": false, "user": {"follow_request_sent": false, "has_extended_profile": false, "profile_use_background_image": true, "default_profile_image": false, "id": 321445166, "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/378800000024702436/c56435230dd53432a3aa7685fdd98cf7.jpeg", "verified": false, "profile_text_color": "333333", "profile_image_url_https": "https://pbs.twimg.com/profile_images/782124224198631424/zuUAjl5o_normal.jpg", "profile_sidebar_fill_color": "EFEFEF", "entities": {"url": {"urls": [{"indices": [0, 23], "expanded_url": "https://youtu.be/1qjR-p_o3BE", "display_url": "youtu.be/1qjR-p_o3BE"}]}, "description": {"urls": []}}, "followers_count": 4992012, "profile_sidebar_border_color": "FFFFFF", "id_str": "321445166", "profile_background_color": "182D66", "listed_count": 5284, "is_translation_enabled": false, "utc_offset": 7200, "statuses_count": 28488, "description": "There's a fine line between being sassy and being an asshole and I cross it everyday. Youtuber and student. Link in bio thatssarcasmposts@gmail.com", "friends_count": 55420, "location": "Cape Town", "profile_link_color": "536BA7", "profile_image_url": "http://pbs.twimg.com/profile_images/782124224198631424/zuUAjl5o_normal.jpg", "following": false, "geo_enabled": false, "profile_banner_url": "https://pbs.twimg.com/profile_banners/321445166/1475082140", "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/378800000024702436/c56435230dd53432a3aa7685fdd98cf7.jpeg", "screen_name": "ThatsSarcasm", "lang": "en", "profile_background_tile": false, "favourites_count": 3931, "name": "joke", "notifications": false, "created_at": "Tue Jun 21 15:52:41 +0000 2011", "contributors_enabled": false, "time_zone": "Pretoria", "protected": false, "default_profile": false, "is_translator": false}, "geo": null, "in_reply_to_user_id_str": null, "lang": "en", "created_at": "Thu Oct 13 19:30:20 +0000 2016", "in_reply_to_status_id_str": null, "place": null}
  {"contributors": null, "truncated": false, "text": "If you're not following @relatabIe for the most relatable tweets ever, then what are you doing?I love their posts", "is_quote_status": false, "in_reply_to_status_id": null, "id": 786649227002781696, "favorite_count": 29, "source": "&lt;a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\"&gt;Twitter for iPhone&lt;/a&gt;", "retweeted": false, "coordinates": null, "entities": {"symbols": [], "user_mentions": [{"id": 485854360, "indices": [24, 34], "id_str": "485854360", "screen_name": "relatabIe", "name": "Relatable Tweets!"}], "hashtags": [], "urls": []}, "in_reply_to_screen_name": null, "in_reply_to_user_id": null, "retweet_count": 6, "id_str": "786649227002781696", "favorited": false, "user": {"follow_request_sent": false, "has_extended_profile": false, "profile_use_background_image": true, "default_profile_image": false, "id": 321445166, "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/378800000024702436/c56435230dd53432a3aa7685fdd98cf7.jpeg", "verified": false, "profile_text_color": "333333", "profile_image_url_https": "https://pbs.twimg.com/profile_images/782124224198631424/zuUAjl5o_normal.jpg", "profile_sidebar_fill_color": "EFEFEF", "entities": {"url": {"urls": [{"indices": [0, 23], "expanded_url": "https://youtu.be/1qjR-p_o3BE", "display_url": "youtu.be/1qjR-p_o3BE"}]}, "description": {"urls": []}}, "followers_count": 4992012, "profile_sidebar_border_color": "FFFFFF", "id_str": "321445166", "profile_background_color": "182D66", "listed_count": 5284, "is_translation_enabled": false, "utc_offset": 7200, "statuses_count": 28488, "description": "There's a fine line between being sassy and being an asshole and I cross it everyday. Youtuber and student. Link in bio thatssarcasmposts@gmail.com", "friends_count": 55420, "location": "Cape Town", "profile_link_color": "536BA7", "profile_image_url": "http://pbs.twimg.com/profile_images/782124224198631424/zuUAjl5o_normal.jpg", "following": false, "geo_enabled": false, "profile_banner_url": "https://pbs.twimg.com/profile_banners/321445166/1475082140", "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/378800000024702436/c56435230dd53432a3aa7685fdd98cf7.jpeg", "screen_name": "ThatsSarcasm", "lang": "en", "profile_background_tile": false, "favourites_count": 3931, "name": "joke", "notifications": false, "created_at": "Tue Jun 21 15:52:41 +0000 2011", "contributors_enabled": false, "time_zone": "Pretoria", "protected": false, "default_profile": false, "is_translator": false}, "geo": null, "in_reply_to_user_id_str": null, "lang": "en", "created_at": "Thu Oct 13 19:26:05 +0000 2016", "in_reply_to_status_id_str": null, "place": null}
  </code></pre>
  
  <p>The python code I used to convert was</p>
  
  <pre><code>import json
  import csv
  
  x=open('MyFile.json')
  data=json.load(x)
  x.close()
  
  f=csv.writer(open('data.csv','wb+')) 
  for item in data:
      f.writerow(item["text"])
  
  f.close()
  </code></pre>
  
  <p>My aim was to write only the text attribute(3rd one) in the csv file but it shows following error</p>
  
  <pre><code>File "to_csv.py", line 10, in &lt;module&gt;
    f.writerow(item["text"])
  UnicodeEncodeError: 'ascii' codec can't encode character u'\U0001f60f' in position 0: ordinal not in range(128)
  </code></pre>
  
  <p>I have searched and found a hint. I replaced line 10 as:</p>
  
  <pre><code>f.writerow(unicode(item["text"]).encode("utf-8"))
  </code></pre>
  
  <p>but to no avail. Can't find a way to keep non-ascii characters intact.</p>
  
  <p>EDIT: thanks to mx0's solution, the code runs fine. one thing i noticed was that there is newline character because of which a single text attribute gets printed in two lines, for example check the text attribute in the first line of json file. there is a newline character between 'boy' and 'little'. i wanted to avoid printing those so i wrote this code</p>
  
  <pre><code>f = csv.writer(open('data.csv', 'wb+'))
  for item in data:
      if item["text"].encode('utf-8')=='\n':
          f.writerow(" ")
      else:
          f.writerow([item["text"].encode('utf-8')])
  </code></pre>
  
  <p>but it doesn't seem to work. anything wrong?</p>