我有一个有3列的csv文件,第三列包含该行的数据类型(Training/PublicTest/PrivateTest(,为了使该数据处于运行状态,我使用if语句检查用法是否等于关键字,然后对该行执行一些操作。出于某种原因,它不会检测到用法,并且每一行都标有用法,拼写/大写是正确的,不知道该怎么办?
编辑由于这个csv文件是针对我目前正在工作的机器学习模型的,它非常庞大,如果我提供链接,它会更容易:https://www.kaggle.com/ashishbansal23/emotion-recognition.至于关于印刷的评论";"用法";,等等,我这样做了,结果只是单词Training、PublicTest或PrivateTest。
for row in open(path):
idx = 0
real_idx = idx + 1
with open(path, "r") as c:
emotion, image, usage = c.readlines()[real_idx].split(",")
if usage == "Trainingn":
train_labels.append(int(emotion))
imageArr = []
imageArr.append(image)
train_images.append(imageArr)
elif usage == "PublicTestn" or usage == "PrivateTestn":
test_labels.append(int(emotion))
imageArr = []
imageArr.append(image)
test_images.append(imageArr)
else:
print("This row was not assigned to any usage!")
idx += 1
def load_data():
return train_images, train_labels, test_images, test_labels
format_data(path)
load_data()```
嗨。我会把CSV文件读成这样的字符串[]。。。
string[] lines = File.ReadAllLines(@csv); // read all CSV lines
int limit = lines.count; // no. of rows
基于"在第一行
System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(",");
string fs = lines[0]; // First string in lines
n = rex.Matches(fs).Count; // how many "," in line
接下来,获取行数和列数。。。
int rows = limit + 1; // no. of rows
int cols = oc + 1; // no. of cols
public static string[,] table; // The CSV table
table = new string[rows, cols]; // CSV table [rows,cols]
然后将每一行分解为单独的值并将其存储在表中,这样您就可以独立地检查每一项
string dummy = "";
string ts = "";
int p = 0;
for (int j = 0; j < rows; j++)
{
ts = lines[r].Trim();
for (int k = 1; k < cols; k++) // break the line into its different parts
{
p = ts.IndexOf(","); // position of 1st comma
if (p == -1)
{
if (ts != "" & ts != null)
table[j, k] = ts.Trim().ToUpper();
else
table[j, k] = "X"; // insert an X if no data
continue;
}
dummy = ts.Substring(0, p);
table[j, k] = dummy.Trim.ToUpper();
}
}
在那里,您有一个具有单独值的数组[n,m],您可以管理