-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']

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

But when i try to process over (ingredients) , it enters in processing as a part of string !! what should i do to prevent that ? @sudomakeinstall2
Don't print the list, print the strings inside the list
Why do u want to prevent that? What is the problem?
@sudomakeinstall2 I used you code and it works fine without u :)) .
@iayork I used print as indication for my process
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.