切换要在 python 的 .csv 阅读器中读取的文件
我多次读取 csv 文件,但每次浏览时都会缩小其大小。因此,一旦到达底部,我就会编写一个新的 csv 文件,即 .csv 文件的下半部分。然后我希望更改 csv 阅读器以使用这个新文件,但它似乎不起作用......这就是我所做的。
sent = open(someFilePath)
r_send = csv.reader(sent)
try:
something = r_send.next()
except StopIteration:
sent.seek(0)
sent.close()
newFile = cutFile(someFilePath, someLineNumber)
sent = open(newFile, "r")
r_send = csv.reader(sent)
cutFile 在哪里..
def cutFile(sender, lines):
sent = open(sender, "r")
new_sent = open(sender + ".temp.csv", "w")
counter = 0
for line in sent:
counter = counter + 1
if counter >= lines:
print >> new_sent, ",".join(line)
new_sent.close()
return sender + ".temp.csv"
为什么这不起作用?
I am reading a csv file several times, but cutting its size every time I go through it. So, once I've reached the bottom, I am writing a new csv file which is, say, the bottom half of the .csv file. I then wish to change the csv reader to use this new file instead, but it doesn't seem to be working... Here's what I've done.
sent = open(someFilePath)
r_send = csv.reader(sent)
try:
something = r_send.next()
except StopIteration:
sent.seek(0)
sent.close()
newFile = cutFile(someFilePath, someLineNumber)
sent = open(newFile, "r")
r_send = csv.reader(sent)
where cutFile does..
def cutFile(sender, lines):
sent = open(sender, "r")
new_sent = open(sender + ".temp.csv", "w")
counter = 0
for line in sent:
counter = counter + 1
if counter >= lines:
print >> new_sent, ",".join(line)
new_sent.close()
return sender + ".temp.csv"
Why is this not working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
something = r_send.next()
是否处于某种循环中?按照你写的方式,它只会读取一行。",".join(line)
?您可以简单地打印line
本身,它应该可以工作。seek(0)
。something = r_send.next()
in some kind of loop? The way you wrote it, it's only going to read one line.",".join(line)
? You can simply printline
itself, and it should work.seek(0)
before closing a file.我建议如下:
使用
for some in r_send:
而不是something = r_send.next()
;您甚至不需要 try... except 块,因为您只需将关闭原始文件的内容放在该循环之外(正如其他人提到的,您甚至没有在当前代码中循环遍历原始文件) 。然后,您可能希望将所有这些内容包装在另一个循环中,以便它继续继续,直到文件被完全操作为止。使用
new_sent.write(line)
而不是print >> new_sent, ",".join(line)
.并不是说除了",".join
位(您不需要它,因为您没有使用csv
模块写入一个文件),无论如何你都不应该在这里使用它,但这使得你正在写入文件的事实更加明显。所以...
类似的事情。
I suggest the following:
Use
for something in r_send:
rather thansomething = r_send.next()
; you won't even need the try... except blocks, as you'll just put the stuff closing the original file outside that loop (as someone else mentioned, you aren't even looping through the original file in your current code). Then you'll probably want to wrap all that in another loop so it keeps continuing until the file has been fully manipulated.Use
new_sent.write(line)
instead ofprint >> new_sent, ",".join(line)
. Not that it makes that much of a difference besides the",".join
bit (which you don't need since you aren't using thecsv
module to write to a file), which you shouldn't be using here anyway, but it makes the fact that you're writing to a file more evident.So...
Something like that.