0

I have textfile as

01,Jay,Sharma
02,Rushi,Patel

I want to read this textfile as array and want output as

student no : 01
Name : Jay
Surname : Sharma
student no : 02
Name : Rushi
Surname : Patel

Actually I am new with the Python I able to read it as array but I want exact output ,Please can anyone help me.

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)

Thank you in advance

Rin
  • 81
  • 1
  • 2
  • 11

5 Answers5

1

There is a library to read csv file in Python: csv. You can read csv file easily with this.

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]))
Ellisein
  • 878
  • 6
  • 17
0

You can try doing it as follows:

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)

I added an if condition, just to keep a check on the number of values in the row_data list. If there are some missing values, it can be processed accordingly.
For a much shorter approach, see @Ellision's answer to this.

taurus05
  • 2,491
  • 15
  • 28
0
class RowReader:
    def fileRead(self,filepath):
        file = open(filepath,"r")
        for line in file:
            row_data = line.strip("\n").split(',')
            print("student no : {}\nName : {}\nSurname : {}".format(
        row_data[0], row_data[1], row_data[2]))

file = RowReader()
file.fileRead("filepath")
rohit prakash
  • 565
  • 6
  • 12
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")

here is the changes in our code

Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
Rahul kuchhadia
  • 289
  • 4
  • 10
0

You can use for in loop for this problem

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]))

and the output will be

student number : "01
Name : Jay
Surname : Sharma"
student number : "02
Name : Rushi
Surname : Patel"
Nikko Bobier
  • 100
  • 11