获取python3中特定字符串部分的数据类型

时间:2018-05-26 13:29:00

标签: python python-3.x csv parsing floating-point

我输入了一个csv文件,如-0.02872239612042904,-0.19755002856254578 ..有128个值,当我从csv文件读取该数组时,它被读为'-0.02872239612042904,-0.19755002856254578 ..'我已经找到了一种方法将所有字符串映射到特定数据类型。现在我这样做: -

result=list(map(float, re.findall(r'\d+', en)))  #en=string read from csv file

但是因为这些是面部编码,并且当计算距离时它会一直返回False,我认为是因为转换为字符串后数组变得像1906684972345829.0等等。

我找不到一个数据类型来表示像-0.02872239612042904这样的数字,这就是为什么在映射时我转换为浮点数是错误的格式。任何人都可以告诉我python3中数字的正确数据类型是什么,如-0.02872239612042904。非常感谢,现在让我头疼。

编辑: - 这就是我从csv文件中读取数据的方式: -

def get_encodings():
    df=pd.read_csv('Encodings/encodings.csv')       #getting file
    with tqdm(total=len(list(df.iterrows()))) as prbar:
        encodings=[]
        images=[]
        for index, row in df.iterrows():
            r=[]
            en=df.loc[index,'Encoding']
            print(en)   #prints correctly
            print(type(en))   #prints string and I want exact same data in its original form which looks like I have shown below

"[-0.19053705  0.06230173  0.04058716 -0.08283613 -0.07159504 -0.10155849
  0.06008045 -0.06842063  0.1317966  -0.10250588  0.203399   -0.01436609
 -0.21249449 -0.09238856  0.0279788   0.08926097 -0.09177385 -0.1628615
 -0.03505187 -0.12979373  0.05772705  0.00208503 -0.06933809  0.00741822
 -0.17499965 -0.25000119 -0.0205064  -0.03139503  0.01130889 -0.1057417
  0.13554846  0.06285821 -0.18908061 -0.02082938  0.04383367  0.23148835
 -0.05068404 -0.00925579  0.1900605  -0.05617992 -0.12842563 -0.06219928
  0.07317995  0.26369438  0.10394366  0.05749369  0.02448226 -0.07668396
  0.1266536  -0.23425353  0.04819498  0.07290804  0.111645    0.08294459
  0.10209186 -0.21581331  0.07399686  0.07748453 -0.22381224  0.01746997
  0.0188249  -0.06403829 -0.07789861 -0.0249712   0.21001905  0.03979192
 -0.12171203 -0.06864078  0.21658717 -0.17392246 -0.06753681  0.09808435
 -0.0076007  -0.18134885 -0.23990698  0.07026891  0.3552466   0.17010394
 -0.16684352  0.03726491  0.02757547  0.01445537  0.10094975  0.04033324
 -0.10441576  0.0377433  -0.09693146  0.04404883  0.16759454  0.0402087
 -0.05915016  0.1369293   0.05408669  0.05787617  0.03509152  0.01340439
 -0.06379045  0.04323686 -0.09738267 -0.02683797  0.14505677 -0.10747927
  0.03247242  0.11747092 -0.18656668  0.22448684 -0.00474619 -0.00586929
 -0.05853979  0.06613642 -0.065335    0.02921261  0.08723848 -0.30918318
  0.23265852  0.20364268 -0.07978678  0.19747412  0.08048097  0.04772019
  0.06427031 -0.03703914 -0.14493702 -0.12132056 -0.01301065 -0.02351468
  0.10600268  0.06480799]"

我的一行数据看起来像这样^我只想要所有这些都没有这种类型的引号dtype('

1 个答案:

答案 0 :(得分:1)

如果您有csv,请使用csv-module读取它(或者读取pandas,它会自动将您的值转换为合适的类型):

创建演示文件:

data =  """-0.02872239612042904, -0.19755002856254578, 0.31345692434, -0.0009348573822
-1.02872239612042904, -1.19755002856254578, 1.31345692434, -1.0009348573822
-2.02872239612042904, -2.19755002856254578, 2.31345692434, -2.0009348573822
-3.02872239612042904, -3.19755002856254578, 3.31345692434, -3.0009348573822
apple, prank, 0.23, nothing
"""

with open("datafile.csv","w") as f:
    f.write(data)

中阅读demofile
def safeFloat(text):
    try:
        return float(text)
    except ValueError: # maybe even catchall here
        return float("nan")

data = []    
import csv
with open("datafile.csv","r") as r:
    csv = csv.reader(r, delimiter=',')
    for l in csv:
        data.append(list(map(safeFloat,l))) # safeFloat to capture errors

print(data)

如果数据中有非浮点数,则可能需要在def safeFloat(text)内使用float而不是map来防止解析错误,而某些文本无法转换为浮点数。

输出:

[[-0.02872239612042904, -0.19755002856254578, 0.31345692434, -0.0009348573822], 
 [-1.028722396120429, -1.1975500285625458, 1.31345692434, -1.0009348573822], 
 [-2.028722396120429, -2.1975500285625458, 2.31345692434, -2.0009348573822], 
 [-3.028722396120429, -3.1975500285625458, 3.31345692434, -3.0009348573822], 
 [nan, nan, 0.23, nan]]

你也可以使用正则表达式,但是你的模式需要允许可选的符号以及它之前/之后的点和数字:

r'[+-]?\d+\.\d+'  # would allow for 123.1245 - but not for 123 or .1234 
                  # would allow an optional +- before numbers

您可以检查模式f.e.在http://regex101.com - 这个带有演示数据的模式可以在这里找到:https://regex101.com/r/xSiyO1/1

pandas解决方案(仅限有效数据):

data =  """-0.02872239612042904, -0.19755002856254578, 0.31345692434, -0.0009348573822
-1.02872239612042904, -1.19755002856254578, 1.31345692434, -1.0009348573822
-2.02872239612042904, -2.19755002856254578, 2.31345692434, -2.0009348573822
-3.02872239612042904, -3.19755002856254578, 3.31345692434, -3.0009348573822
"""

with open("datafile.csv","w") as f:
    f.write(data)

import pandas as pd
import numpy as np

df = pd.read_csv("datafile.csv", dtype={"a":np.float64,"b":np.float64,"c":np.float64,"d":np.float64},names=["a","b","c","d"] )
print(df) 

输出:

      a        b         c         d
0 -0.028722 -0.19755  0.313457 -0.000935
1 -1.028722 -1.19755  1.313457 -1.000935
2 -2.028722 -2.19755  2.313457 -2.000935
3 -3.028722 -3.19755  3.313457 -3.000935