首页 \ 问答 \ 在文件中编写从JSON获得的Python字典(Write Python dictionary obtained from JSON in a file)

在文件中编写从JSON获得的Python字典(Write Python dictionary obtained from JSON in a file)

我有这个脚本从网页中抽象出json对象。 json对象被转换为字典。 现在我需要在文件中编写这些词典。 这是我的代码:

#!/usr/bin/python

import requests

r = requests.get('https://github.com/timeline.json')
for item in r.json or []:
    print item['repository']['name']

文件中有十行。 我需要在该文件中编写包含十行的字典..我该怎么办? 谢谢。


I have this script which abstract the json objects from the webpage. The json objects are converted into dictionary. Now I need to write those dictionaries in a file. Here's my code:

#!/usr/bin/python

import requests

r = requests.get('https://github.com/timeline.json')
for item in r.json or []:
    print item['repository']['name']

There are ten lines in a file. I need to write the dictionary in that file which consist of ten lines..How do I do that? Thanks.


原文:
更新时间:2022-08-30 17:08

最满意答案

actonBlock1是一个没有链接任何东西的TransformBlock 。 块生成的任何项目都将保留在其输出缓冲区中,在这个cas中只有数字1.在项目停留在输出中的情况下,块永远不能完成。 您可以根据您需要的方式修复几种不同的方法。

1)将TransformBlock更改为一个ActionBlock

2)将TransformBlock链接到NullTarget或另一个块。


actonBlock1 is a TransformBlock that is not linked to anything. Any items that the block produces will remain in its output buffer, in this cas only the number 1. With items stuck in the output the block can never complete. You can fix that a couple of different ways depending on what exactly you need.

1) Change the TransformBlock to an ActionBlock

2) Link the TransformBlock to a NullTarget or another block.

相关问答

更多
  • 首先,您根本不需要TPL Dataflow,因为您实际上没有任何数据流。 其次,像这样使用TaskScheduler也不是正确的解决方案。 TaskScheduler的调度代码 ,但在await某些东西时,没有代码在运行。 因此,当WriteAsync()正在进行异步工作时, Open()代码可以运行。 你实际需要的东西就像ReaderWriterLock ,但是同样适用于async 。 框架中没有类似的东西,但你可以使用Stephen Toub的文章构建异步协调基元,第7部分:AsyncReaderWri ...
  • 在TPL Dataflow中,您可以使用DataflowLinkOptions 链接块 ,并指定块完成的传播 : request.LinkTo(fetch, new DataflowLinkOptions { PropagateCompletion = true }); fetch.LinkTo(commit, new DataflowLinkOptions { PropagateCompletion = true }); 之后,您只需为request块调用Complete()方法,就完成了! // th ...
  • 由于您的代码现在,您需要分别在所有块上调用Complete : s_Batch.Complete(); await s_Batch.Completion; s_Action.Complete(); await s_Action.Completion; 但是,您可以使用DataflowLinkOptions来链接块以请求完成传播: s_Batch.LinkTo(s_Action, new DataflowLinkOptions {PropagateCompletion = true}); s_Batch. ...
  • 首先,正如@usr建议的那样,您的案例适合单个ActionBlock 。 链接多个块的关键是组成具有不同配置的不同类型的块。 例如,如果你有一步, MaxDegreeOfParallelism为1,另一步为10。 其次, MaxDegreeOfParallelism设置最大值,这意味着TPL可以根据资源的可用性选择该范围内的任何数字。 所以我不担心设置所有这些块的最大值,如果TPL决定使用更少的线程,它会更好。 从如何:指定数据流块中的并行度 由于MaxDegreeOfParallelism属性表示最大并行 ...
  • 回答完问题后(见下文),我意识到使用TPL Dataflow块对您的设计进行建模可能不是一个好主意。 TDF适用于基本上独立的块处理消息,没有内置的跟踪单个消息的方式。 但这就是你想要的东西:处理程序按顺序处理消息,跟踪每条消息的完成情况。 因此,我认为您不应该创建整个数据流网络,而是使用单个ActionBlock作为异步消息处理器: public class Bus { class Subscription { public Guid Id { get; private s ...
  • TPL Dataflow从框架本身转移到NuGet。 所以,现在,要从您的项目中使用它,您需要从NuGet获取它(右键单击References→Manage NuGet packges)。 这种变化的原因是TPL Dataflow可以更频繁地更新,而不仅仅是在整个框架更新时。 TPL Dataflow was moved from the framework itself to NuGet. So, now, to use it from your project, you need to get it f ...
  • actonBlock1是一个没有链接任何东西的TransformBlock 。 块生成的任何项目都将保留在其输出缓冲区中,在这个cas中只有数字1.在项目停留在输出中的情况下,块永远不能完成。 您可以根据您需要的方式修复几种不同的方法。 1)将TransformBlock更改为一个ActionBlock 2)将TransformBlock链接到NullTarget或另一个块。 actonBlock1 is a TransformBlock that is not linked to anything. An ...
  • 只有在从生产者发布所有项目之后,消费者才会处于活动状态。 异步意味着生成和使用任务都可以并行运行。 这是因为您在消费者有机会开始之前非常快速地发布了所有项目。 如果您添加了Thread.Sleep(100) ,您会看到它们实际上并行工作。 给定消费者的睡眠时间以验证其是否阻止其他数据项。 它似乎是顺序执行而没有任何并行性。 TPL Dataflow并不神奇:它不会修改您的代码以便并行执行。 是你调用AscTransConsumerAsync()一次,所以不要惊讶它实际只执行一次。 TDF确实支持并行处理,但 ...
  • 我将首先考虑该类的通用接口应该是什么样子。 我认为最简单的解决方案是: public class SplitBlock { public ITargetBlock Input { get; } public ISourceBlock LeftOutput { get; } public ISourceBlock RightOutput { get ...
  • 在Windows Phone上支持TPL数据流是我们的积压工作。 它不受支持的原因是因为尚未为Windows Phone实现其中一个依赖项(即并发集合)。 但是,我们相信我们可以删除此依赖项或端口并发集合。 我已经为它提交了一个用户语音项目 。 请分享和投票 - 这有助于我们确定这项工作的优先顺序。 更新 我很高兴地宣布,我们发布了包含对Windows Phone 8支持的预览版 。 Supporting TPL Dataflow on Windows Phone is on our backlog. Th ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)