[英]how to read csv file with missing columns and remove first and last delimiter of each row?
Looks like someone just saved lists of python with a csv extension.看起来有人刚刚保存了带有 csv 扩展名的 python 列表。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.