从Python中的制表符分隔数据中删除双引号

问题描述 投票:0回答:1

我在 csv 文件中存储了一些用 dab 分隔的数据,我正在尝试打开 csv 文件并在分隔符“ ”上分隔数据。但在数据中存在一些额外的引号,我无法获得所需的输出并面临数据问题。需要一些快速帮助。

输入样本数据

"id name    class   age school  doj dol status  source"
"001    sandeep 10  16  dav 2012.12.12  2023.12.12  passed  database"
"002    rahul   09  15  ximb    2023.11.11  ""inprogress    manual"
"003    aditya  12  18  kmbb    2024.01.12  ""inprogress    schoolrecord"
"004    ved ""09    15  ximb    2022.11.11  2023.12.13  passed  manual"

enter image description here

代码:

import pandas as pd 
file='data_tab_delimited.csv'
data = pd.read_csv(file,sep="\t")
print(data)

data.to_csv('school.csv')

输出即将到来:

,id name    class   age school  doj dol status  source
0,001   sandeep 10  16  dav 2012.12.12  2023.12.12  passed  database
1,"002  rahul   09  15  ximb    2023.11.11  ""inprogress    manual"
2,"003  aditya  12  18  kmbb    2024.01.12  ""inprogress    schoolrecord"
3,"004  ved ""09    15  ximb    2022.11.11  2023.12.13  passed  manual"

所需输出:


    id     name  class  age school         doj         dol    status        source
0  001  sandeep     10   16    dav  2012.12.12  2023.12.12    passed     database
1  002    rahul      9   15   ximb  2023.11.11         NaN  inprogress      manual
2  003   aditya     12   18   kmbb  2024.01.12         NaN  inprogress  schoolrecord
3  004      ved    NaN   15   ximb  2022.11.11  2023.12.13    passed       manual

输入原始数据:

"Equipment Number   Equipment Desc  Equipment category  Type of Technical Object    Technical Object Desc   Object number   Maintenance Plan    PLANT   Planner Group   Planner Group Desc  Work Center Work Center Desc    ABC indicator   Maintenance plant   LOCATION    Location Desc   Valid To Date   Start-up Date   Manufacturer serial number  Manufacturer model number   Manufacturer part number    Manufacturer of asset   COUNTRY Year of construction    Month of construction   ROOM    Sort field  Cost Center Catalog Profile Catalog Profile Desc    Superordinate Equipment Guarantee date  Warranty ends   Created on  FUNCTION_LOCATION   STATUS  SOURCE_ID"



"0000101    U02 GENANC RELAY PANEL  K   PWELE-OBJ   ELECTRICAL OBJECTS  IE0567      5010    TM2 Ture Mai-Elec   EGXX1   ELECTRICAL MAINT. Unit-2(GENEANC)   C   5XX SXX     9999.12.31      ""G9876.PG1/0ABC            XX ABXD CO. LTD                     50MAABC PWABC   ELEC SYS GEN                2011.12.15      INACTIVE    DUMMY"

请提供一些帮助。

python python-3.x pandas dataframe read.csv
1个回答
0
投票

修复您的 csv 文件可能就是您正在寻找的内容,因为您提供的内容没有用制表符分隔。

此格式:

id  name    class   age school  doj dol status  source
001 sandeep 10  16  dav 2012.12.12  2023.12.12  passed  database
002 rahul   09  15  ximb    2023.11.11  ""  inprogress  manual
003 aditya  12  18  kmbb    2024.01.12  ""  inprogress  schoolrecord
004 ved ""09    15  ximb    2022.11.11  2023.12.13  passed  manual

用制表符分隔每个字段,并删除产生此输出的不必要的引号:

,id,name,class,age,school,doj,dol,status,source
0,1,sandeep,10,16,dav,2012.12.12,2023.12.12,passed,database
1,2,rahul,9,15,ximb,2023.11.11,,inprogress,manual
2,3,aditya,12,18,kmbb,2024.01.12,,inprogress,schoolrecord
3,4,ved,9,15,ximb,2022.11.11,2023.12.13,passed,manual

呈现如下:

id 名字 班级 年龄 学校 doj 多尔 状态 来源
0 1 沙深 10 16 达夫 2012年12月12日 2023年12月12日 通过了 数据库
1 2 拉胡尔 9 15 ximb 2023.11.11 进行中 手册
2 3 阿迪亚 12 18 kmbb 2024.01.12 进行中 学校记录
3 4 9 15 ximb 2022.11.11 2023年12月13日 通过了 手册
© www.soinside.com 2019 - 2024. All rights reserved.