切换要在 python 的 .csv 阅读器中读取的文件

发布于 2024-09-08 19:09:03 字数 986 浏览 2 评论 0原文

我多次读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒带 2024-09-15 19:09:04
  1. something = r_send.next() 是否处于某种循环中?按照你写的方式,它只会读取一行。
  2. 为什么需要",".join(line)?您可以简单地打印 line 本身,它应该可以工作。
  3. 另外,在关闭文件之前确实不需要seek(0)
  1. Is something = r_send.next() in some kind of loop? The way you wrote it, it's only going to read one line.
  2. Why do you need ",".join(line)? You can simply print line itself, and it should work.
  3. Plus, there really is no need to seek(0) before closing a file.
烏雲後面有陽光 2024-09-15 19:09:04

我建议如下:

使用 for some in r_send: 而不是 something = r_send.next();您甚至不需要 try... except 块,因为您只需将关闭原始文件的内容放在该循环之外(正如其他人提到的,您甚至没有在当前代码中循环遍历原始文件) 。然后,您可能希望将所有这些内容包装在另一个循环中,以便它继续继续,直到文件被完全操作为止。

使用 new_sent.write(line) 而不是 print >> new_sent, ",".join(line).并不是说除了 ",".join 位(您不需要它,因为您没有使用 csv 模块写入一个文件),无论如何你都不应该在这里使用它,但这使得你正在写入文件的事实更加明显。

所以...

sent = open(someFilePath)
r_send = csv.reader(sent)

someLineNumber = len(sent.readlines())

while someLineNumber > 0:
    for something in r_send:
        # do stuff

    someLineNumber /= 2     # //= 2 in Python 3

    sent.close()
    newFile = cutFile(someFilePath, someLineNumber)
    sent = open(newFile, "r")
    r_send = csv.reader(sent)

类似的事情。

I suggest the following:

Use for something in r_send: rather than something = 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 of print >> 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 the csv 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...

sent = open(someFilePath)
r_send = csv.reader(sent)

someLineNumber = len(sent.readlines())

while someLineNumber > 0:
    for something in r_send:
        # do stuff

    someLineNumber /= 2     # //= 2 in Python 3

    sent.close()
    newFile = cutFile(someFilePath, someLineNumber)
    sent = open(newFile, "r")
    r_send = csv.reader(sent)

Something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文