Home > Back-end >  how to read csv file with missing columns and remove first and last delimiter of each row?
how to read csv file with missing columns and remove first and last delimiter of each row?

Time:01-21

Dataset looks like :

enter image description here

how to read this and remove first and last delimiter of each row?

CodePudding user response:

Looks like someone just saved lists of python with a csv extension. Maybe you can try to read those list and then transform them to a data frame. Just like this

with open('file.csv','r') as file:
 data = file.readlines()
df = pd.DataFrame(data=data)

Any ways looks like your data doesn't have the same number of columns for each row.

CodePudding user response:

Something like this maybe?

import re

with open('yourFile.csv','r') as f:
    lines = f.readlines()

data = []
for line in lines:
    print(line)
    line = re.sub(r"\[|\]|\"|\n|'","",line) #match whatever you want to remove.
    print(line)
    data.append(line.split(","))

print(data)
  •  Tags:  
  • Related