麻烦的Python操作/循环顺序(Trouble with Python order of operations/loop)
我有一些代码用于将CSV文件转换为制表符分隔文件。 我的问题是我无法弄清楚如何以正确的顺序写出正确的值。 这是我的代码:
for file in import_dir: data = csv.reader(open(file)) fields = data.next() new_file = export_dir+os.path.basename(file) tab_file = open(export_dir+os.path.basename(file), 'a+') for row in data: items = zip(fields, row) item = {} for (name, value) in items: item[name] = value.strip() tab_file.write(item['name']+'\t'+item['order_num']...) tab_file.write('\n'+item['amt_due']+'\t'+item['due_date']...)
现在,由于我的两个
write
语句都在for row in data
循环中的for row in data
,因此我的头文件被多次写入。
如果我突然出现第一个write
语句,我将会出现明显的格式错误。
如果我将第二个write
语句移到第一个然后突出的上面,我的数据就会出现故障。
我该怎么做才能确保第一个write
语句作为标题写入一次,第二个写入为CSV文件中的每一行写入? 如何在不破坏字典的情况下在循环外提取第一个'write'语句? 谢谢!I have some code that is meant to convert CSV files into tab delimited files. My problem is that I cannot figure out how to write the correct values in the correct order. Here is my code:
for file in import_dir: data = csv.reader(open(file)) fields = data.next() new_file = export_dir+os.path.basename(file) tab_file = open(export_dir+os.path.basename(file), 'a+') for row in data: items = zip(fields, row) item = {} for (name, value) in items: item[name] = value.strip() tab_file.write(item['name']+'\t'+item['order_num']...) tab_file.write('\n'+item['amt_due']+'\t'+item['due_date']...)
Now, since both my
write
statements are in thefor row in data
loop, my headers are being written multiple times over.
If I outdent the firstwrite
statement, I'll have an obvious formatting error.
If I move the secondwrite
statement above the first and then outdent, my data will be out of order.
What can I do to make sure that the firstwrite
statement gets written once as a header, and the second gets written for each line in the CSV file? How do I extract the first 'write' statement outside of the loop without breaking the dictionary? Thanks!
原文:https://stackoverflow.com/questions/16845878
最满意答案
您可以使用setInterval方法在每个固定时间延迟上调用函数,在大多数情况下,它将与clearInterval方法一起用于停止重复调用,这里是一个示例:
var interval = setInterval( function () { afoo( function (err) { // clear interval if an error occured if ( err ) { console.log(err); clearInterval(interval); } }); }, 5 * 60 * 1000);
如果你想要更多的控制,那么使用node-cron模块,它与linux系统使用的cron模式相同,例如:
const CronJob = require('cron').CronJob; // run afoo function every 15 min var job = new CronJob('00 15 * * * *', afoo);
You could use setInterval method to call a function on every fixed time delay, in the most case it's used in conjunction with clearInterval method to stop the repeated calls, here is an example:
var interval = setInterval( function () { afoo( function (err) { // clear interval if an error occured if ( err ) { console.log(err); clearInterval(interval); } }); }, 5 * 60 * 1000);
If you want more control then use node-cron module, it has the same cron pattern used by linux systems, Example:
const CronJob = require('cron').CronJob; // run afoo function every 15 min var job = new CronJob('00 15 * * * *', afoo);
相关问答
更多-
矛盾的计时功能(Paradoxal timing functions)[2023-04-07]
JVM上的nanoTime被认为是安全的,但不是很准确。 这里有一些很好的读物: System.nanoTime()完全没用吗? System.currentTimeMillis vs System.nanoTime 基本上你的测试会受到计时器不准确的影响。 解决这个问题的一种方法是time("outerpos")调用time("outerpos") (注意JIT编译器优化可能会在某个时刻启动)并仅测量一次开始和结束时间。 取平均值并重复time("innerpos") 。 这就是我所能想到的。 仍然不是有 ... -
我建议您查看内置的Python剖析器( profile或cProfile ,具体取决于您的需求): http : cProfile Instead of writing your own profiling code, I suggest you check out the built-in Python profilers (profile or cProfile, depending on your needs): http://docs.python.org/library/profile.html
-
正如Mark Rejhon的回答中所提到的,现代浏览器中提供了一个API,可以将亚毫秒分辨率时序数据暴露给脚本: W3C高分辨率定时器 ,即window.performance.now() 。 now()比传统的Date.getTime()有两个重要的方面: now()是一个双倍的亚毫秒分辨率,代表页面导航开始以来的毫秒数。 它返回分数的微秒数(例如,1000.123的值为1秒和123微秒)。 now()单调增加。 这很重要,因为Date.getTime() 可能在后续调用时向前或向后跳转。 值得注意的是, ...
-
您将返回once()函数,而不是promise链。 基本上,你写的是这样的: //returns the once promise return ref.once('value', function(snap) { /* some stuff that doesn't affect the promise chain happens here */ }); 当你想要的是这样的: // returns whatever happens in the .then() callback return re ...
-
您可以使用setInterval方法在每个固定时间延迟上调用函数,在大多数情况下,它将与clearInterval方法一起用于停止重复调用,这里是一个示例: var interval = setInterval( function () { afoo( function (err) { // clear interval if an error occured if ( err ) { console.log(err); ...
-
计时bash功能(Timing bash functions)[2021-11-13]
您可以使用命令分组而不是子shell: time { command1; command2; } 更新 - 更具体的示例: test-function() { time { rsync -av ~/tmp/test-dir-start/ ~/tmp/test-dir-end/ rsync -av ~/tmp/test-dir-start/ ~/tmp/test-dir-end-2/ # etc... } } You can use command grou ... -
如评论中所示,性能计时插件使用s.performanceTiming(); 触发实际的插件。 这就是说它应该如何配置才能正常工作。 s.pte是用于确定是否执行实际插件的调用。 `s.pte = 'event10,event11,event12,event13,event14,event15,event16,event17,event18,event19' //[--------------------------- 1 to 8 ---------------------------][-- 9 --][ ...
-
VB6中定时功能/测量性能的最佳方法是什么?(What's the best way of timing functions / measuring performance in VB6?)[2021-06-13]
我通常使用Windows hihg分辨率性能计数器。 查看QueryPerformanceCounter和QueryPerfomanceFrequency 通常我有一个简单的类,其构造函数和析构函数调用QueryPerformanceCounter,然后将差异添加到运行总计。 对于工具,请查看devpartner 。 虽然它运行良好,但检测大量代码会使我的应用程序运行速度极慢。 我通常发现我希望只在一两个函数上得到精确的时序,所以我经常最终使用性能计数器函数而不是使用devpartner。 I typica ... -
您只能使用命令行客户端psql使用\timing ,因为这是一个psql命令。 它是一个开关,可以打开和关闭执行时间报告: test=> \timing Timing is on. test=> SELECT 42; ┌──────────┐ │ ?column? │ ├──────────┤ │ 42 │ └──────────┘ (1 row) Time: 0.745 ms test=> \timing Timing is off. You can use \timing only wi ...
-
在计算机Jquery中使用其他函数时,循环更改的时间(Timing for loop changes when using other functions in the computer Jquery)[2023-02-10]
你可以做的一件事是将一个设置超时放在另一个设置中。 现在你有setTimeout(,时间); setTImeout(,time * 2); setTImeout(,time * 3); 也许如果你把它改成:setTimeout(... setTimeout(... setTimeout(...,time),time),time); 所以这将确保执行顺序。 第一个将永远执行。 One thing that you can do is put one set timeout inside another one ...