首页 > 解决方案 > 在“:”上拆分字符串时如何处理日期和时间

问题描述

我正在处理一个文本文件,逐行读取,然后将其插入数据库。每一行都像

3530000000000:100000431506294:Jean:Camargo:male::::Kefron:6/4/2018 12:00:00 AM::11/19

问题是它还拆分了日期时间,因此它在数据库中填充了错误的信息,如下图所示。

在此处输入图像描述

我的代码如下:

 with open(filename, encoding="utf-8") as f:
        counter = 0
        for line in f:
            data = line.split(':')
            
            id = str(counter)
            Phonenumber = data[0].strip()
            profileID = data[1].strip()
            firstname = data[2].strip()
            secondname = data[3].strip()
            gender = data[4].strip()
            LocationWhereLive = data[5].strip()
            LocationWhereFrom = data[6].strip()
            RelationshipStatus = data[7].strip()
            whereWork = data[8].strip()
            AccountCreationDate = data [9].strip()
            Email = data[10].strip()
            Birthdate = data [11].strip()
            
            
            
            
            
            mycursor = mydb.cursor()
            sql = mycursor.execute("insert into dataleads values ('"+id+"','"+Phonenumber+"','"+profileID+"','"+firstname+"','"+secondname+"','"+gender+"','"+LocationWhereLive+"','"+LocationWhereFrom+"','"+RelationshipStatus+"','"+whereWork+"','"+AccountCreationDate+"','"+Email+"','"+Birthdate+"')")
            mycursor.execute(sql)
       
            mydb.commit()
            
            counter += 1

标签: python

解决方案


除了按空格分割,您还可以在 split 和 rsplit 方法中利用 maxsplit 参数:

def make_list(s):
    before = s.split(":", maxsplit= 9) # splits up to the date
    after = before[-1].rsplit(":", maxsplit= 2)  # splits the last part up to the date (from the right)
    return [*before[:-1], *after] # creates a list with both parts

s = "3530000000000:100000431506294:Jean:Camargo:male::::Kefron:6/4/2018 12:00:00 AM::11/19"

make_list(s)
Out: 
['3530000000000',
 '100000431506294',
 'Jean',
 'Camargo',
 'male',
 '',
 '',
 '',
 'Kefron',
 '6/4/2018 12:00:00 AM',
 '',
 '11/19']

推荐阅读