-2

I try to convert from json to csv, But there is Extra letter "u" has been appeared before every word in list , i used pandas to read this csv data, this is my code :

import json
import csv
with open("train.json") as file:
    data = json.load(file)
with open("trainc.csv", "w") as file:
    csv_file = csv.writer(file)
    csv_file.writerow(data[0].keys())
    for item in data:
    csv_file.writerow(item.values())
import pandas as pd
train = pd.read_csv("trainc.csv", header=0)

As Example from json file, this is the first one :

{
    "id": 10259,
    "cuisine": "greek",
    "ingredients": [
      "romaine lettuce",
      "black olives",
      "grape tomatoes",
      "garlic",
      "pepper",
      "purple onion",
      "seasoning",
      "garbanzo beans",
      "feta cheese crumbles"
    ]
  }

I used this line to print ingredients

print train['ingredients'][0] 

And when I printed the same record output was like that :

[u'romaine lettuce', u'black olives', u'grape tomatoes', u'garlic', u'pepper', u'purple onion', u'seasoning', u'garbanzo beans', u'feta cheese crumbles']

Saeid
  • 4,147
  • 7
  • 27
  • 43

1 Answers1

3

This u is not in your string. It just says that the type of data is unicode.

for x in train['ingredients'][0]:
     print x

You see that there in no extra u in your data.

Python str vs unicode types
http://www.diveintopython.net/xml_processing/unicode.html

Community
  • 1
  • 1
Saeid
  • 4,147
  • 7
  • 27
  • 43