Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
384 views
in Technique[技术] by (71.8m points)

pandas - Python: Issues in retaining the exact format of string while uploading data from pipe delimited text to database

I have a raw .txt file that contains the following fields and data

ID     MaterialCode    Vendor_Code
1      00033456        1990
2      4455600         02567

I am uploading this data to postgresql using sqlalchemy. I am using the following code:

conn = sqlalchemy.create_engine('conn_strng')
outputdype_app = sqlcol_append_stg(dataframe)
df = pd.read_csv(f,usecols=col_lst,sep='|',converters = 
     {'MaterialCode':'str','Vendor_Code':'str'},engine='python',encoding='iso-8859-1')
df.to_sql(schema=schema_name,name=table_name,con = conn,if_exists = 'append', method = 'multi', index=False,dtype=outputdype_app)

The outputdtype_app is being derived as follows:

def sqlcol_append_stg(dfparam):
    dtypedict_app = {}
    for i,j in zip(dfparam.columns, dfparam.dtypes):
        if "datetime" in str(j):
           dtypedict_app.update({i: sqlalchemy.types.DateTime()})
        else:
           dtypedict_app.update({i: sqlalchemy.types.VARCHAR()})
    return dtypedict_app

When I am seeing the DB, I see the 0 prefixes in MaterialCode and Vendor_Code are gone! I see the following:

ID     MaterialCode    Vendor_Code
1      33456           1990
2      4455600         2567

How can I fix this issue? I want exact same string in DB.

question from:https://stackoverflow.com/questions/65943771/python-issues-in-retaining-the-exact-format-of-string-while-uploading-data-from

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...