我在将信息与txt文件分离并将其添加到列表并使用该信息创建对象方面遇到任何问题
此应用程序的数据文件将以逗号分隔(每条数据将以a分隔 逗号),纯文本文件。标记单个等级类型(实验室,项目和测试等级)之间的差异 空的数据字段将被包括在内。文件的第一行将包含可能的要点 练习。学生ID将为字符串。
示例 ID,名字,姓氏,10、10、10、100、100、100、100
三个实验室(每个都得10分)
两个项目(每个价值100分)
一次测试(价值100分)
txt文件设置如下
ID,名字,姓氏,10、10、10、100、100、100
1234,Joe,Student,10,8,9,,100,90,,100
5678,另一个,学生,7,7,7,,75,75,,75
9012,还,另一个,10,10,10,,65,70,,50
对象类
public class Student {
private String identifcation;
private String firstName;
private String lastName;
private List labs;
private List project;
private List test;
public Student(String identifcation, String firstName, String lastName, List<Integer> labs, List<Integer> projects, List<Integer> test) {
this.identifcation = identifcation;
this.firstName = firstName;
this.lastName = lastName;
this.labs = labs;
this.project = projects;
this.test = test;
}
}
类中的readfile方法
public List<Student> decode() {
List<Student> students = new ArrayList<Student>();
while (this.scan.hasNext()) {
List<Integer> labs = new ArrayList<Integer>();
List<Integer> projects = new ArrayList<Integer>();
List<Integer> tests = new ArrayList<Integer>();
String identifcation = this.scan.next();
String firstName = this.scan.next();
String lastName = this.scan.next();
//help int lab = the labs
//help labs.add(lab); should add the 3 labs
//help int project =
//help projects.add(project); should add the 2 projects
//help int test =
//help tests.add(test); should add the 1 test
System.out.println(identifcation);
System.out.println(firstName);
System.out.println(lastName);
System.out.print(this.scan.nextLine());
Student real = new Student(identifcation, firstName, lastName, labs, projects, tests);
students.add(real);
}
return students;
}
打印语句给我输出
ID,第一个
名字,最后
名称,10、10、10、100、100、100、100
1234,Joe,Student,7,8,9,,80,90,,100
5678,另一个,学生,7,7,7,,75,75,,75
9012,还,另一个,10,10,10,,65,70,,50
他们应该给予
ID
名字
姓氏
等等。
答案 0 :(得分:0)
我建议一行一行地插入扫描仪-将整行存储为输入字符串。然后,您可以使用Java string.split()之类的函数拆分行,并在分隔符选项中添加','。这将允许您返回数组中的字符串,然后可以根据需要将其排序到数组列表中。有关split()函数的更多详细信息,请参见here
示例(可能要检入编译器):
import glob
import pandas as pd
import numpy as np
files = glob.glob('folder/*.csv')
for file in files:
df = pd.read_csv(file)
#make conversion
df['Time taken'] = pd.to_datetime(df['Time taken'])
df['Time taken'] = df['Time taken'].dt.hour + df['Time taken'].dt.minute / 60
#output file
df.to_csv('updated_{}'.format(file), index = False)
希望这会有所帮助!
答案 1 :(得分:0)
您可以将带分隔符的String标记器用作“,”。您只需要逐行读取文件并将其标记化即可获取对象。我会这样写。
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
class Student {
private String identifcation;
@Override
public String toString() {
return "Student{" +
"identifcation='" + identifcation + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", labs=" + labs +
", project=" + project +
", test=" + test +
'}';
}
private String firstName;
private String lastName;
private List labs;
private List project;
private List test;
public Student(String identifcation, String firstName, String lastName, List<Integer> labs, List<Integer> projects, List<Integer> test) {
this.identifcation = identifcation;
this.firstName = firstName;
this.lastName = lastName;
this.labs = labs;
this.project = projects;
this.test = test;
}
}
public class Main {
public static void main(String[] args) {
String line = "example ID,First Name,Last Name,10,10,10,,100,100,,100";
StringTokenizer tokenizer = new StringTokenizer(line,",");
while(tokenizer.hasMoreElements()) {
String id = tokenizer.nextToken();
String fname = tokenizer.nextToken();
String lName = tokenizer.nextToken();
List<Integer> labs = new ArrayList<Integer>(){{
add(Integer.parseInt(tokenizer.nextToken()));
add(Integer.parseInt(tokenizer.nextToken()));
add(Integer.parseInt(tokenizer.nextToken()));
}};
List<Integer> project = new ArrayList<Integer>(){{
add(Integer.parseInt(tokenizer.nextToken()));
add(Integer.parseInt(tokenizer.nextToken()));
}};
List<Integer> test = new ArrayList<Integer>(){{
add(Integer.parseInt(tokenizer.nextToken()));
}};
Student s1 = new Student(id,fname,lName,labs,project,test);
System.out.println(s1.toString());
}
}
}