[英]File is not converting from json to jsonl correctly
Have a project to collect data from Instagram account and collect engagement.有一个从 Instagram 帐户收集数据并收集参与度的项目。 The scraper returns the data as a json file.刮板将数据作为 json 文件返回。 [Here][1] is that file. [这里][1] 是那个文件。 My function is to locate the number of likes for types of posts and store it in a list.我的功能是定位帖子类型的喜欢数量并将其存储在列表中。 Then sum that list and divide by the number of followers for that account that's bound to a variable.然后对该列表求和并除以绑定到变量的该帐户的关注者数量。 The issue I'm having is converting the json file to a jsonl file in order to iterate over it.我遇到的问题是将 json 文件转换为 jsonl 文件以便对其进行迭代。 Here's my code:这是我的代码:
honda_follower_count = 4_200_000
def engagement(filename, follower_count):
"""Return the brand post engagement for the Instagram metadata file,
filename, given follower_count as the number of Instagram followers for
the brand.
Returns a decimal engagement rate rounded to 4 decimal places. Python's
standard `round` function should be used. E.g.:
>>> engagement('instagram/volvocars.json', volvocars_follower_count)
0.0125
"""
engagement = [] #set empty list for like counts
data = filename #store filename to variable
with open(data, 'w') as outfile: #save open file as outfile
for entry in outfile:
json.dump(entry, outfile)
outfile.write('\n')
for line in outfile:
line_object = json.load(line)
likes = line_object["edge_media_preview_like"]["count"] #find count
if num in likes > 1:
engagement.append(num) #add count to engagement
comments = line_object["edge_media_to_comment"]["count"] #find other count
if nums in comments > 1:
engagement.append(nums) #add count
engage_perc = sum(engagement)/follower_count #sum all counts and divide by the number of followers
return engage_perc
When I test it using:当我使用以下方法对其进行测试时:
engagement(INSTAGRAM_DIR /'honda.json', honda_follower_count)
Error: <ipython-input-6-7070dc1e8678> in engagement(filename, follower_count)
13 data = filename #store filename to variable
14 with open(data, 'w') as outfile: #save open file as outfile
---> 15 for entry in outfile:
16 json.dump(entry, outfile)
17 outfile.write('\n')
UnsupportedOperation: not readable
datadir = Path('drive/My Drive/APRD6342/Data') datadir = Path('驱动器/我的驱动器/APRD6342/数据')
#create folder to store instagram file #创建用于存储Instagram文件的文件夹
INSTAGRAM_DIR = datadir / 'instagram' INSTAGRAM_DIR = 数据目录 / 'instagram'
Ideally the function should return a number that's the sum of all the likes divided by the number of followers but I either get a poxipath file error or the not readable error.
The file itself is stored on my google drive in
[1]: http://ge.tt/2nR5Je73
Thanks for all your help.感谢你的帮助。 I was able to figure out how to just take the whole json at once.我能够弄清楚如何一次获取整个 json。
def engagement(filename, follower_count):
"""Return the brand post engagement for the Instagram metadata file,
filename, given follower_count as the number of Instagram followers for
the brand.
Returns a decimal engagement rate rounded to 4 decimal places. Python's
standard `round` function should be used. E.g.:
>>> engagement('instagram/volvocars.json', volvocars_follower_count)
0.0125
"""
engagement = [] #set empty list for like counts
data = filename #store filename to variable
with open(data) as f: #save open file as outfile
parse = json.load(f)
for n in range(0,200):
likes = parse["GraphImages"][n]["edge_media_preview_like"]["count"] #find count
if likes > 1:
engagement.append(likes) #add count to engagement
comments = parse["GraphImages"][n]["edge_media_to_comment"]["count"] #find other count
if comments > 1:
engagement.append(comments) #add count
engage_perc = (sum(engagement)/len(range(0,200)))/follower_count #sum all counts and divide by the number of followers
return round(engage_perc,4)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.