如何按行使用CSV文件的大小?是否可以使用Python

发布于 2025-01-27 19:35:17 字数 291 浏览 4 评论 0原文

我正在阅读一个没有PANDAS库的CSV文件,我需要找出每一行的字节数。

file="Emptycsv.csv"
checkfilesize(file)

def checkfilesize(file):
 file=b''join(file).split(b'\n')
 count=len(file)

计数将打印行数。我需要找出文件大小,我知道os.path.getsize会..但是,在读取CSV文件行的读取该文件行/列或列时,如果达到109997897KB,我需要终止因此,请您帮助我查找数据字节

I am reading a csv file without pandas library,i need to find out the number of bytes of each row.So i can ad all the bytes and determine the file size and will do some restriction based on that.

file="Emptycsv.csv"
checkfilesize(file)

def checkfilesize(file):
 file=b''join(file).split(b'\n')
 count=len(file)

Count will print the number of rows.i need to find out the file size,i know os.path.getsize will do..But while reading that csv file row by rows/column or columns if it reached 109997897kb,i need to terminate.So request you to help me the find the bytes of data

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

和我恋爱吧 2025-02-03 19:35:17

按照此方面的某些内容应适用于每行/行的字节:
(+1用于新线)

import os

total_bytes = -1

with open("test.csv") as file_in:
    for line in file_in:
        bytes_on_this_line = len(line) + 1
        total_bytes += bytes_on_this_line
        
print(total_bytes)

print(os.path.getsize("test.csv"))

输出:

15
15

Something along the lines of this should work for getting the bytes per row/line:
(The +1 is for the newline)

import os

total_bytes = -1

with open("test.csv") as file_in:
    for line in file_in:
        bytes_on_this_line = len(line) + 1
        total_bytes += bytes_on_this_line
        
print(total_bytes)

print(os.path.getsize("test.csv"))

Output:

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