我的文本文件为
01,Jay,Sharma
02,Rushi,Patel
我想将此文本文件读取为数组,并希望输出为
student no : 01
Name : Jay
Surname : Sharma
student no : 02
Name : Rushi
Surname : Patel
实际上,我是Python的新手,我能够将其读取为数组,但我想要准确的输出,请任何人都可以帮助我。
class RowReader:
def fileRead(self,filepath):
textfile = open(filepath, 'r')
data = []
for line in textfile:
row_data = line.strip("\n").split(',')
print(row_data)
file = RowReader()
file.fileRead(file_path)
提前谢谢
答案 0 :(得分:1)
有一个库可以在Python中读取csv文件:csv
。
您可以轻松地读取csv文件。
import csv
data = []
with open("filename.csv", "r") as f:
reader = csv.reader(f)
for line in reader:
data.append(line)
for d in data:
print("student no : {}\nName : {}\nSurname : {}".format(
d[0], d[1], d[2]))
答案 1 :(得分:0)
您可以尝试如下操作:
class RowReader:
def fileRead(self,filepath):
textfile = open(filepath, 'r')
data = []
for line in textfile:
row_data = line.strip("\n").split(',')
if len(row_data) == 3:
print("student no : {}\nName : {}\nSurname : {}".format(
row_data[0], row_data[1], row_data[2]))
file = RowReader()
file.fileRead(file_path)
我添加了一个if条件,只是为了检查row_data列表中的值数量。如果存在一些缺失值,可以对其进行相应处理。
有关更简短的方法,请参见@Ellision对此的回答。
答案 2 :(得分:0)
public class playercontrol: MonoBehaviour {
public void Update() {
playera.transform.Translate((Vector3.forward * speed * Time.deltaTime));
Debug.Log("speed" + speed);
if (check == false) {
if (Input.GetMouseButtonDown(0)) {
Debug.Log("left button");
firstPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
}
if (Input.GetMouseButtonUp(0)) {
secondPressPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
currentSwipe.Normalize();
//swipe left
if (currentSwipe.x < -1.0 f && currentSwipe.y > -0.5 f && currentSwipe.y < 0.5 f) {
Debug.Log("left swipe");
if (transform.position.x > -9 f) {
spos = transform.position;
Debug.Log("spos" + spos);
endpos = new Vector3(transform.position.x - 9 f, transform.position.y, transform.position.z + ((speed * 2) / 20));
Debug.Log("speed......... ......." + ((speed * 5) / 20));
transform.rotation = Quaternion.Euler(0, -10.0 f, 0);
check = true;
//endpos * (Time.deltaTime*0.5f);
// Time.timeScale = 0.5f;
// to.rotation = Quaternion.Euler (0, 0, 0);
}
}
//swipe right
if (currentSwipe.x > 1.0 f && currentSwipe.y > -0.5 f && currentSwipe.y < 0.5 f) {
Debug.Log("right swipe");
if (transform.position.x < 9 f) {
Debug.Log("spos1" + spos);
spos = transform.position;
endpos = new Vector3(transform.position.x + 9 f, transform.position.y, transform.position.z + ((speed * 2) / 20));
Debug.Log("speed1......... ......." + ((speed * 5) / 20));
transform.rotation = Quaternion.Euler(0, 10.0 f, 0);
check = true;
// Time.timeScale = 0.5f;
// to.rotation = Quaternion.Euler (0, 0, 0);
}
}
}
}
if (check == true) {
time += 10 * Time.deltaTime;
transform.position = Vector3.Lerp(spos, endpos, time);
// transform.rotation = Quaternion.Lerp (from.rotation, to.rotation, time);
if (time > 1) {
transform.rotation = Quaternion.Euler(0, 0, 0);
//Time.timeScale = 1.0f;
//playera.transform.Rotate (0, 0, 0);
//transform.Rotate (0, 0, 0);
check = false;
time = 0;
}
}
}
}
答案 3 :(得分:0)
class RowReader:
def fileRead(self,filepath):
textfile = open(filepath, 'r')
data = []
for line in textfile:
row_data = line.strip("\n").split(',')
data.append(row_data)
#print(row_data)
for x in range(len(data)):
print("student no :",data[x][0])
print("Name :",data[x][1])
print("Surname :",data[x][2])
#print(data)
file = RowReader()
file.fileRead("new.txt")
这是我们代码中的更改
答案 4 :(得分:0)
您可以在循环中使用此问题
import csv
output=[]
with open('filename.csv','r') as file:
fileCsv = csv.reader(file)
for row in file:
rowData = row.strip('\n').split(',')
output.append(rowData)
file.close()
for anotherRow in output:
print('student number : %s\nName : %s\nSurname : %s'%(anotherRow[0],anotherRow[1],anotherRow[2]))
输出将是
student number : "01
Name : Jay
Surname : Sharma"
student number : "02
Name : Rushi
Surname : Patel"