简体   繁体   English

如何读取缺少列的 csv 文件并删除每行的第一个和最后一个分隔符?

[英]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.看起来有人刚刚保存了带有 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.

相关问题 如何将 csv 文件读入熊猫,跳过行直到某个字符串,然后选择第一行作为标题和分隔符作为 | - How to read csv file into pandas, skipping rows until a certain string, then selecting first row after as header and delimiter as | 如何使用熊猫在最后一个字段中存在分隔符的情况下读取CSV文件? - How to use Pandas to read CSV file with delimiter existing in the last field? 如何在 csv 文件中的列 [2] 的每一行中删除分隔符 (.) 后的符号? - How to remove symbols after delimiter (.) in every row of column [2] in csv file? 如何删除 csv 文件中的第一行? - How to remove first row in csv file? Pandas 按分隔符将每一行分成两列 (5GB CSV) - Pandas split each row by delimiter into two columns (5GB CSV) 如何使用python获取CSV文件最后一行的第一个值 - How to get the first value of last row of a CSV file using python 如何用“||”替换第一个和最后一个“,”分隔符来自 csv 而其他人保持原样? - How to replace the first and the last ',' delimiter with '||' from a csv while leaving the others as it is? 如何从文件的每一行中删除第一个和最后 n 个字符 - How to remove first and last n characters from each line of a file 如何读取 pandas 中缺少分隔符的 csv(或 - 带有其他分隔符) - How to read a csv in pandas with a missing delimiter (or - with additional delimiters) 如何从csv文件读取前两列 - how to read first two columns from csv file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM