[英]Load all rows in csv file - Python
I want to load a csv file into python.我想将一个 csv 文件加载到 python 中。 The csv file contains grades for a random number of students and a random number of assignments.
csv 文件包含随机数量的学生和随机数量的作业的成绩。
I want python to delete the header and the first column (Name of student) and this is my code:我希望python删除标题和第一列(学生姓名),这是我的代码:
with open("testgrades.csv") as f:
ncols = len(f.readline().split(','))
nrows = sum(1 for row in f)
grades = np.loadtxt("testgrades.csv", delimiter=',', skiprows=1, usecols=range(1,ncols+1))
print(document1)
The code works for columns but can't handle if I add one or more rows in the csv file?该代码适用于列,但如果我在 csv 文件中添加一行或多行就无法处理?
My CSV file:我的 CSV 文件:
And output from Python:和 Python 的输出:
Your csv
image looks like a messed up spread sheet image.您的
csv
图像看起来像一个乱七八糟的电子表格图像。 It isn't a copy of the csv
file itself, which is plain text.它不是
csv
文件本身的副本,它是纯文本。 You should be able to copy-n-paste that text to your question.您应该能够将该文本复制粘贴到您的问题中。
The Output
image is an array, with numbers that correspond to the first 6 rows of the csv
image. Output
图像是一个数组,其数字对应于csv
图像的前 6 行。
Your question is not clear.你的问题不清楚。 I'm guessing you added the last 2 rows to the spread sheet, and are having problems loading those into
numpy
.我猜您将最后 2 行添加到电子表格中,并且在将它们加载到
numpy
遇到问题。 I don't see anything wrong with those numbers in the spread sheet image.我认为电子表格图像中的这些数字没有任何问题。 But if you show the actual csv file content, we might identify the problem.
但是,如果您显示实际的 csv 文件内容,我们可能会发现问题所在。 Maybe you aren't actually writing those added rows to the csv file.
也许您实际上并没有将这些添加的行写入 csv 文件。
Your code sample, with corrected indentation is:您的代码示例,更正缩进是:
with open("testgrades.csv") as f:
ncols = len(f.readline().split(','))
nrows = sum(1 for row in f)
grades = np.loadtxt("testgrades.csv", delimiter=',', skiprows=1, usecols=range(1,ncols+1))
print(grades)
I can see using the ncols
to determine the number of columns.我可以看到使用
ncols
来确定列数。 The usecols
parameter needs an explicit list of columns, not some sort of all-but-first
. usecols
参数需要一个明确的列列表,而不是某种all-but-first
。 You could have also gotten that number from a plain loadtxt
(or genfromtxt
).您也可以从普通的
loadtxt
(或genfromtxt
)中获得该数字。
But why calculate nrows
?但是为什么要计算
nrows
呢? You don't appear to use it.你似乎没有使用它。 And it isn't needed in the
loadtxt
.在
loadtxt
不需要它。 genfromtxt
allows a max_rows
parameter if you need to limit the number of rows read.如果您需要限制读取的行数,
genfromtxt
允许使用max_rows
参数。
Python has a special module for reading and writing CSV files Python CSV Python 有一个特殊的模块用于读写 CSV 文件Python CSV
Python 2蟒蛇 2
import csv
with open('testgrades.csv', 'rb') as f:
Python 3蟒蛇 3
import csv
with open('testgrades.csv', newline='') as f:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.