我有一个字符串如下:
2017-11-27T09:59:57.278-06:00,,"[0.2094101093721778, -65.0, -76.0]"
2017-11-27T10:00:17.250-06:00,,"[0.13055123127828835, -62.0, -76.0]"
我想在我的数据框中有以下内容:
09:59:57.278 0.2094101093721778 -65.0 -76.0
10:00:17.250 0.13055123127828835 -62.0 -76.0
我试图将第一个值删除为:
a = "2017-11-27T09:59:57.278-06:00,,\"[0.2094101093721778, -65.0, -76.0]\""
b = a.strip("2017-11-27T")
我得到以下输出:
9:59:57.278-06:00,,"[0.2094101093721778, -65.0, -76.0]"
我其实想要09:59:57.278-06:00,,"[0.2094101093721778, -65.0, -76.0]"
答案 0 :(得分:1)
您的strip
删除了所提供字符的所有组合,因此它还从0
删除了前一个09
。您可能希望执行以下操作之一:
a = "2017-11-27T09:59:57.278-06:00,,\"[0.2094101093721778, -65.0, -76.0]\""
b = a.replace("2017-11-27T","")
或强>
b = ''.join(a.split("2017-11-27T")[1:])
输出(适用于两者)
'09:59:57.278-06:00,,"[0.2094101093721778, -65.0, -76.0]"'
如果您有不同的日期(并且硬编码通常是一种不好的做法),您可能希望将该字符串的该段转换为datetime
对象并在字符串中再次表示它:
t = a.split(",")
t[0] = datetime.datetime.strftime(datetime.datetime.strptime(t[0][0:-6], "%Y-%m-%dT%H:%M:%S.%f"),"%H:%M:%S.%f")
b = ''.join(t)
如果它适用于您的DataFrame,最好的方法可能就是用pandas
来解释日期。有关详细信息,请参阅this link。
答案 1 :(得分:1)
你可以试试这个
import pandas as pd
lin = '2017-11-27T09:59:57.278-06:00,,"[0.2094101093721778, -65.0, -76.0]"\n 2017-11-27T10:00:17.250-06:00,,"[0.13055123127828835, -62.0, -76.0]"'
chrToReplace = [',,','[',']','"',',']
y =[]
# Iterate through your lines
for x in lin.splitlines():
for c in chrToReplace:
if c in x:
x = x.replace(c," ")
x= x.split()
n = 0
z ={}
for elm in x:
z.update({"V"+str(n):elm})
n += 1
y.append(z)
df = pd.DataFrame(y)
print(df)
这会给你
V0 V1 V2 V3
0 2017-11-27T09:59:57.278-06:00 0.2094101093721778 -65.0 -76.0
1 2017-11-27T10:00:17.250-06:00 0.13055123127828835 -62.0 -76.0