简体   繁体   中英

how to read csv file with missing columns and remove first and last delimiter of each row?

Dataset looks like:

在此处输入图像描述

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

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM