Friday, April 21, 2023

Demo of splitlines and split (Problem on Strings)

myfile.csv

student_id,student_name,percentage 1,Ram,50 2,Jim,60 3,Jack,70 4,Jones,80 5,Rahim,90

Code

with open("myfile.csv", mode = 'r') as f: data = f.read() print(data) print(type(data)) # <class 'str'> data = data.splitlines() # Splits the string at line breaks and returns a list print("List of strings") print(data) print(type(data)) # <class 'list'> # Using list comprehension # data = [x.split(',') for x in data] output = [] for i in data: output.append(i.split(",")) print("List of list") print(output)
Tags: Technology,Python,

No comments:

Post a Comment