在文件中编写从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.
原文:
最满意答案
actonBlock1
是一个没有链接任何东西的TransformBlock
。 块生成的任何项目都将保留在其输出缓冲区中,在这个cas中只有数字1.在项目停留在输出中的情况下,块永远不能完成。 您可以根据您需要的方式修复几种不同的方法。1)将
TransformBlock
更改为一个ActionBlock
2)将
TransformBlock
链接到NullTarget
或另一个块。
actonBlock1
is aTransformBlock
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 anActionBlock
2) Link the
TransformBlock
to aNullTarget
or another block.
相关问答
更多-
首先,您根本不需要TPL Dataflow,因为您实际上没有任何数据流。 其次,像这样使用TaskScheduler也不是正确的解决方案。 TaskScheduler的调度代码 ,但在await某些东西时,没有代码在运行。 因此,当WriteAsync()正在进行异步工作时, Open()代码可以运行。 你实际需要的东西就像ReaderWriterLock ,但是同样适用于async 。 框架中没有类似的东西,但你可以使用Stephen Toub的文章构建异步协调基元,第7部分:AsyncReaderWri ...
-
完成TPL数据流循环(Completion in TPL Dataflow Loops)[2022-03-29]
在TPL Dataflow中,您可以使用DataflowLinkOptions 链接块 ,并指定块完成的传播 : request.LinkTo(fetch, new DataflowLinkOptions { PropagateCompletion = true }); fetch.LinkTo(commit, new DataflowLinkOptions { PropagateCompletion = true }); 之后,您只需为request块调用Complete()方法,就完成了! // th ... -
完成TPL数据流链(Completing a TPL dataflow chain)[2020-01-14]
由于您的代码现在,您需要分别在所有块上调用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 ...