麻烦将结果写入文件 [英] trouble writing results to files

查看:53
本文介绍了麻烦将结果写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个半相关的问题...


首先,我试图使用

csv将字符串列表输出到csv文件模块。输出文件用

逗号分隔字符串的每个字母,然后将每个字符串放在一个单独的行上。所以代码是:


import csv

output = csv.writer(open(''/ Python25 / working / output.csv'',''' a'')

a = [" apple"," cranberry"," tart"]

for elem in range(len(a)):

output.writerow(a [elem])

....它会写入文件:

a,p,p,l ,e

c,r,a,n,b,e,r,r,y

t,a,r,t


如何让它写apple,蔓越莓,tart等?


其次,

程序运行完毕和文本实际显示在
之间有一个显着的延迟(5-10分钟) />
文件。为什么会出现这种情况的想法?使用

csv模块或标准方式书写也一样。


谢谢!

Lisa

解决方案

li **** ******@gmail.com 写道:


import csv

output = csv.writer(open( ''/Python25/working/output.csv'',''a''))

a = [" apple"," cranberry"," tart"]

for elem in range(len(a)):

output.writerow(a [elem])



output.writerow期望序列作为参数。您正在传递一个

字符串,这是一个字符序列。顺便问一下,你想要得到什么产出?b $ b?你想要一个只有一行的文件(苹果,

蔓越莓,蛋挞),或者每个水果在不同的行中吗?


BTW,迭代范围(len(a))是Python中的反模式。你应该这样做:b $ b这样做:


for a item中的项目:

output.writerow([item])


其次,

程序运行完毕和文本实际出现在之间有一个显着的延迟(5-10分钟) br />
文件。



尝试明确关闭文件。

干杯,

-

Roberto Bonvallet


2006-11-29,Roberto Bonvallet< Ro *************** @ cern.chwrote:


BTW,迭代范围(len(a))是Python中的反模式。



除非你正在修改a的元素,否则肯定会吗?


-

Neil Cerutti

你不能给他那个削减车道。他太快了,他看得很清楚。如果他得到一点点破解,他还可以逃避你。 --Dick Lebeau


Neil Cerutti写道:


2006-11-29,Roberto Bonvallet< Ro***************@cern.chwrote:


> BTW,迭代范围(len(a))是Python中的反模式。



除非你正在修改a的元素,否则肯定?



枚举是你的朋友:)


代表n,枚举项目(a):

如果f(项目):

a [n] =无论


-

Roberto Bonvallet


I have two semi related questions...

First, I am trying to output a list of strings to a csv file using the
csv module. The output file separates each letter of the string with a
comma and then puts each string on a separate line. So the code is:

import csv
output = csv.writer(open(''/Python25/working/output.csv'', ''a''))
a = ["apple", "cranberry", "tart"]
for elem in range(len(a)):
output.writerow(a[elem])
.... and it would write to the file:
a,p,p,l,e
c,r,a,n,b,e,r,r,y
t,a,r,t

How do I get it to write "apple", "cranberry", "tart" ?

Second, there is a significant delay (5-10 minutes) between when the
program finishes running and when the text actually appears in the
file. Any ideas for why this happens? It is the same for writing with
the csv module or the standard way.

thanks!
Lisa

解决方案

li**********@gmail.com wrote:

import csv
output = csv.writer(open(''/Python25/working/output.csv'', ''a''))
a = ["apple", "cranberry", "tart"]
for elem in range(len(a)):
output.writerow(a[elem])

output.writerow expects a sequence as an argument. You are passing a
string, which is a sequence of characters. By the way, what output are you
expecting to get? Do you want a file with only one line (apple,
cranberry, tart), or each fruit in a different line?

BTW, iterating over range(len(a)) is an anti-pattern in Python. You should
do it like this:

for item in a:
output.writerow([item])

Second, there is a significant delay (5-10 minutes) between when the
program finishes running and when the text actually appears in the
file.

Try closing the file explicitly.
Cheers,
--
Roberto Bonvallet


On 2006-11-29, Roberto Bonvallet <Ro***************@cern.chwrote:

BTW, iterating over range(len(a)) is an anti-pattern in Python.

Unless you''re modifying elements of a, surely?

--
Neil Cerutti
You can''t give him that cutback lane. He''s so fast, and he sees it so well. He
can also run away from you if he gets a little bit of crack. --Dick Lebeau


Neil Cerutti wrote:

On 2006-11-29, Roberto Bonvallet <Ro***************@cern.chwrote:

>BTW, iterating over range(len(a)) is an anti-pattern in Python.


Unless you''re modifying elements of a, surely?

enumerate is your friend :)

for n, item in enumerate(a):
if f(item):
a[n] = whatever

--
Roberto Bonvallet


这篇关于麻烦将结果写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆