Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将JSONL文件转换为CSV-“;JSONDecodeError:额外数据“;_Python_Json_Csv_Tweepy - Fatal编程技术网

Python 将JSONL文件转换为CSV-“;JSONDecodeError:额外数据“;

Python 将JSONL文件转换为CSV-“;JSONDecodeError:额外数据“;,python,json,csv,tweepy,Python,Json,Csv,Tweepy,我使用tweepy的Streamlistener收集Twitter数据,我使用的代码生成一个包含大量元数据的JSONL文件。 现在我想把这个文件转换成一个CSV文件,我为它找到了一个代码。不幸的是,我遇到了如下错误: raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 7833) 我阅读了其他线程,我认为这与json.loads无法

我使用tweepy的
Streamlistener
收集Twitter数据,我使用的代码生成一个包含大量元数据的JSONL文件。 现在我想把这个文件转换成一个CSV文件,我为它找到了一个代码。不幸的是,我遇到了如下错误:

raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 7833)
我阅读了其他线程,我认为这与
json.loads
无法处理json文件中的多个部分数据有关(当然,我的json列表文件就是这样)。 如何在代码中规避此问题?或者我必须使用完全不同的方法来转换文件?(我使用的是Python3.6,我流的推特大多是阿拉伯语)


如果数据文件由多行组成,每行都是一个json对象,则可以使用生成器一次解码一行

def extract_json(fileobj):
    # Using "with" ensures that fileobj is closed when we finish reading it.
    with fileobj:
        for line in fileobj:
            yield json.loads(line)
对代码的唯一更改是没有显式读取
data\u json
文件,
data\u python
是调用
extract\u json
而不是
json.loads
的结果。以下是修改后的代码:

import json
import csv
import io

'''
creates a .csv file using a Twitter .json file
the fields have to be set manually
'''

def extract_json(fileobj):
    """
    Iterates over an open JSONL file and yields
    decoded lines.  Closes the file once it has been
    read completely.
    """
    with fileobj:
        for line in fileobj:
            yield json.loads(line)    


data_json = io.open('stream_____.jsonl', mode='r', encoding='utf-8') # Opens in the JSONL file
data_python = extract_json(data_json)

csv_out = io.open('tweets_out_utf8.csv', mode='w', encoding='utf-8') #opens csv file


fields = u'created_at,text,screen_name,followers,friends,rt,fav' #field names
csv_out.write(fields)
csv_out.write(u'\n')

for line in data_python:

    #writes a row and gets the fields from the json object
    #screen_name and followers/friends are found on the second level hence two get methods
    row = [line.get('created_at'),
           '"' + line.get('text').replace('"','""') + '"', #creates double quotes
           line.get('user').get('screen_name'),
           unicode(line.get('user').get('followers_count')),
           unicode(line.get('user').get('friends_count')),
           unicode(line.get('retweet_count')),
           unicode(line.get('favorite_count'))]

    row_joined = u','.join(row)
    csv_out.write(row_joined)
    csv_out.write(u'\n')

csv_out.close()

我从内容上理解你的答案,但从技术上讲,我如何设置数据?我是否必须根据我的数据更改字母或数字?那么,我是否只将建议代码的前两个博客作为我的代码的第一行?希望这些不是很幼稚的问题(新手)。再次感谢!(顺便说一句,在运行它时,我遇到了一条语法错误消息)@JosephinaK。我已经修改了答案,删除了令人困惑的用法示例,并添加了代码的完整版本。它很有效!!!谢谢。我不得不对原始代码做一些小的更改(将unicode更改为str,并添加utf-8-sig for excel),但现在它运行良好:)最后一件事:当打开文件时,读者似乎会混淆包含“=”的文本,并将它们分隔成一个单独的列,从而打乱了整个顺序。“你知道怎么克服这个吗?”约瑟芬纳克。这可能会成为一个新问题,但打开哪个文件?您创建的jsonl或csv?它位于已创建的csv文件中!
import json
import csv
import io

'''
creates a .csv file using a Twitter .json file
the fields have to be set manually
'''

def extract_json(fileobj):
    """
    Iterates over an open JSONL file and yields
    decoded lines.  Closes the file once it has been
    read completely.
    """
    with fileobj:
        for line in fileobj:
            yield json.loads(line)    


data_json = io.open('stream_____.jsonl', mode='r', encoding='utf-8') # Opens in the JSONL file
data_python = extract_json(data_json)

csv_out = io.open('tweets_out_utf8.csv', mode='w', encoding='utf-8') #opens csv file


fields = u'created_at,text,screen_name,followers,friends,rt,fav' #field names
csv_out.write(fields)
csv_out.write(u'\n')

for line in data_python:

    #writes a row and gets the fields from the json object
    #screen_name and followers/friends are found on the second level hence two get methods
    row = [line.get('created_at'),
           '"' + line.get('text').replace('"','""') + '"', #creates double quotes
           line.get('user').get('screen_name'),
           unicode(line.get('user').get('followers_count')),
           unicode(line.get('user').get('friends_count')),
           unicode(line.get('retweet_count')),
           unicode(line.get('favorite_count'))]

    row_joined = u','.join(row)
    csv_out.write(row_joined)
    csv_out.write(u'\n')

csv_out.close()