python - 在“:”上拆分字符串时如何处理日期和时间
问题描述
我正在处理一个文本文件,逐行读取,然后将其插入数据库。每一行都像
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
解决方案
除了按空格分割,您还可以在 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']
推荐阅读
- css - Iphone 上奇怪的 CSS 行为
- java - 需要知道为什么我无法计算名为“empStream”的流中的元素
- php - 如何基于无登录密码插件使用无密码管理员
- android - 为什么我的通知有时不会出现在状态栏中?
- python - 从给定索引数组的Python列表中提取子列表的最快方法
- c++ - 使用 std::bind 并存储到 std:: 函数中
- c# - 创建表(如果不存在)并插入数据无异常处理方式
- typescript - 如何在 TypeScript 中引用属于联合类型的类型?
- php - 无法在 VSCode for PHP 中更改 DocBlock 注释颜色
- javascript - 如何输入文本区域并将该文本添加到 contenteditable div?