0
{"a":"1","b":"1","c":"1"}
{"a":"2","b":"2","c":"2"}
{"a":"3","b":"3","c":"3"}
{"a":"4","b":"4","c":"4"}

I have tried the following code but it gives error:-

from nltk.twitter import Twitter
from nltk.twitter.util import json2csv

with open('C:/Users/Archit/Desktop/raw_tweets.json', 'r') as infile:
# Variable for building our JSON block
json_block = []

for line in infile:

    # Add the line to our JSON block
    json_block.append(line)

    # Check whether we closed our JSON block
    if line.startswith('{'):

        # Do something with the JSON dictionary
        json2csv(json_block, 'tweets.csv', ['id','text','created_at','in_reply_to_user_id','in_reply_to_screen_name','in_reply_to_status_id','user.id','user.screen_name','user.name','user.location','user.friends_count','user.followers_count','source'])

        # Start a new block
        json_block = []

Error:

File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode raise ValueError(errmsg("Expecting value", s, err.value)) from None ValueError: Expecting value: line 1 column 1 (char 0)

6
  • Where are you getting that json2csv function? Commented Oct 29, 2015 at 13:56
  • sorry didnt get what u said ? Commented Oct 29, 2015 at 13:57
  • Sorry, I'll be a bit more explicit. You're using the json2csv function, but that is not defined anywhere. Are you importing it from somewhere? When I run your code, I get NameError: name 'json2csv' is not defined. Commented Oct 29, 2015 at 13:58
  • yep ... from nltk.twitter.util import json2csv Commented Oct 29, 2015 at 14:00
  • That is relevant information and should be included in your question. Commented Oct 29, 2015 at 14:01

3 Answers 3

1
import csv, json

data = []

with open('C:\Users\Shahriar\Desktop\T.txt') as data_file:    
    for line in data_file:
        data.append(json.loads(line))


keys = data[0].keys()

with open('data.csv', 'wb') as csvF:
    csvWriter = csv.DictWriter(csvF, fieldnames=keys)
    csvWriter.writeheader()
    for d in data:
        csvWriter.writerow(d)

Output:

a,c,b
1,1,1
2,2,2
3,3,3
4,4,4
Sign up to request clarification or add additional context in comments.

Comments

1

This is way too late but I also stumbled upon some errors today. I figured that you actually have to import from nltk.twitter.common instead of util. Hope this helps others who stumbled upon this thread

Comments

0
# Read json 
filename = 'C:/Users/Archit/Desktop/raw_tweets.json'
lines = [line.replace("{", "").replace("}", "").replace(":", ",") for line in open(filename)]

# Write csv
with open('out.csv', 'w') as csv_file:
   for line in lines:
      csv_file.write("%s\n" % line)

4 Comments

raceback (most recent call last): File "C:\Users\Archit\Desktop\csv_conv.py", line 35, in <module> csv_file.write("%s\n" % line) io.UnsupportedOperation: not writable
"a","1","b","1","c","1" is this your output csv?
@ArchitGarg thank you, it was missing the opening mode
@AerofoilKite Yes. The OP didn't specify the output format. So, it is safer to to include the quotes in case the elements have commas, like "0", "0,5", "1"...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.