使用导出到csv文件无法导出字符串值'NA'

2024-09-27 17:31:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试合并一些.xls文件,并使用pandas将合并的内容导出到.csv文件中。一切都很好,除了一件事,我有一个列命名为标签,其中包含一个字符串“NA”(缩写为不适用)。很明显,在final.csv中,我期望NA的列中有Null

我希望“NA”不是什么引起出口问题的保留词。我可以手动将包含“NA”的数据帧导出到.csv文件中。这是密码。包含“NA”的列是标签

import pandas as pd
import glob
from time import gmtime, strftime

path= input("Please enter the location of DataModel Spec/s:-> ")

GLB_DM_VER = input("Please enter global DM version:-> ")

GLB_DM_ENV = input("Please enter the global DM version environment:-> ")


datasource_id = "DSN_GMS"


dt=strftime("%Y%m%d%H%M", gmtime())

file_list = glob.glob(path+"\*.xls")

excels = [pd.ExcelFile(name) for name in file_list] 

frames_standards = [x.parse(sheet_name='Data Model 0', 
header=0,index_col=None) for x in excels]

frames_codelist = [y.parse(sheet_name='DataDictionaries', 
header=0,index_col=None) for y in excels]

combined_std = pd.concat(frames_standards)

combined_std.insert(loc=0, column = 'DATASOURCE_ID', value = datasource_id )

combined_std.insert(loc=1, column = 'Global_DM_VERSION_ID', value = 
   GLB_DM_VER )

combined_std.insert(loc=2, column = 'Global_DM_VERSION_ENV', value = 
  GLB_DM_ENV )
combined_std['LOAD_ID'] = dt







 combined_std['LENGTH'].replace(regex=True,inplace=True,to_replace=r'\'',value=r'')

#combined['LENGTH'] = combined['LENGTH'].str.replace("'","")

combined_codelist = pd.concat(frames_codelist)

order = len(combined_codelist.index)+1
ordr_range = list(range(1,order))

combined_codelist.insert(loc=0, column = 'DATASOURCE_ID', value = datasource_id )

combined_codelist.insert(loc=1, column = 'Global_DM_VERSION_ID', value = GLB_DM_VER )

combined_codelist.insert(loc=2, column = 'Global_DM_VERSION_ENV', value = GLB_DM_ENV )

combined_codelist_move = combined_codelist.pop('Decoded Value')

combined_codelist['LABEL']= combined_codelist_move

combined_codelist['CODELIST_ITEM_ORDER'] = ordr_range

combined_codelist['LOAD_ID'] = dt


combined_std.to_csv("STANDARDS.csv", header=['DATASOURCE_ID','Global_DM_VERSION_ID','Global_DM_VERSION_ENV','TARGET_DOMAIN','SOURCE_DOMAIN','DOMAIN_LABEL','SOURCE_VARIABLE','RAVE_LABEL','TYPE','VARIABLE_LENGTH','CONTROL_TYPE','CODELIST_OID','TARGET_VARIABLE','MANDATORY','ORIGIN','LOAD_ID'], index=False)

combined_codelist.to_csv("CODELIST.csv", header=['DATASOURCE_ID','Global_DM_VERSION_ID', 'Global_DM_VERSION_ENV','CODELIST_OID','CODED_VALUE','LABEL','CODELIST_ITEM_ORDER', 'LOAD_ID'], index=False)

Tags: csvenvidvalueversioncolumndmglobal

热门问题