Python 3.10中的字符串到浮点型,字符串的格式是什么?

zbq4xfa0  于 2022-12-15  发布在  Python
关注(0)|答案(2)|浏览(118)

我想用Python 3.10把一个字符串转换成一个浮点数。
问题在于字符串的格式。例如:

"  3.841-11"

其代表3.841E-011。
我试过古典音乐

float("  3.841-11")

但这会产生误差。
只是改变这一个字符串是没有解决办法,因为我想读一个更大的文件,像这样:

$$
$$  GRID Data
$$

GRID           1        -44.0332667.9   -2.55271
GRID           2        -39.1406667.9   -2.26907
GRID           3        -34.2481667.9   -1.98544
GRID           4        -29.3555667.9   -1.70181
GRID           5        -24.4629667.9   -1.41817
GRID           6        -19.5703667.9   -1.13454
GRID           7        -14.6777667.9   -.850903
GRID           8        -9.78516667.9   -.567269
GRID           9        -4.89258667.9   -.283634
GRID          10        3.055-13667.9   3.841-11
GRID          11        4.892579667.9   .2836343

这是我的代码:

def read_fem(location):  
        mesh = open(location, 'r').read().splitlines()
    
        point = []
    
        for i in range(1, len(mesh)):
            if '$' not in mesh[i]:
    
                if 'GRID' in mesh[i]:
                    number = int(mesh[i][8:16])
                    x = float(mesh[i][24:32])
                    y = float(mesh[i][32:40])
                    z = float(mesh[i][40:48])
    
                    point.append([number, x, y, z])

感谢每一个答案。

guicsvcw

guicsvcw1#

使用pandas

IMO,最好不要手动解析你的文件,而是使用一个库。pandas是最理想的。开始阅读你的文件pandas.read_fwf,然后replace-(或+)前面的数字正确的指数形式,然后转换为浮点:

import pandas as pd

data = pd.read_fwf('data.txt', sep='\s+', colspecs=[(8,16),(24,32),(32,40),(40,48)])

# fix the incorrect numbers
# and convert to float
data.update(data.select_dtypes(exclude='number')
                .apply(lambda s: s.str.replace(r'(?<=\d)(?=[-+])', 'e', regex=True)
                                  .astype(float))
            )

# convert to list
out = data.to_numpy().tolist()

输出:

[[1, -44.0332, 667.9, -2.55271],
 [2, -39.1406, 667.9, -2.26907],
 [3, -34.2481, 667.9, -1.98544],
 [4, -29.3555, 667.9, -1.70181],
 [5, -24.4629, 667.9, -1.41817],
 [6, -19.5703, 667.9, -1.13454],
 [7, -14.6777, 667.9, -0.850903],
 [8, -9.78516, 667.9, -0.567269],
 [9, -4.89258, 667.9, -0.283634],
 [10, 3.055e-13, 667.9, 3.841e-11],
 [11, 4.892579, 667.9, 0.2836343]]

旧答案

在转换为float之前,将-替换为e-

s = '3.841-11'

out = float(s.replace('-', 'e-'))

输出:3.841e-11
如果您也可以使用e+,则更通用的方法是使用正则表达式:

import re

s = '3.841+4'

out = float(re.sub(r'(?=[-+])', 'e', s))

输出:38410.0

修复文件的短程序

此函数读取data.txt并输出data_clean.txt中的固定浮点数。

import re
with open('data.txt') as f, open('data_clean.txt', 'w') as f_out:
    f_out.write(re.sub(r'(?<=\d)(?=[-+])', 'e', f.read()))

我还看到了3.055-13667.9这样的数字,这是无效的,因为指数必须是整数。

import re
with open('data.txt') as f, open('data_clean.txt', 'w') as f_out:
    f_out.write(re.sub(r'(?<=\d)([-+])(\d+.\d?)', lambda m: f'e{m.group(1)}{int(float(m.group(2)))}', f.read()))

固定文件:

$$
$$  GRID Data
$$

GRID           1        -44.0332667.9   -2.55271
GRID           2        -39.1406667.9   -2.26907
GRID           3        -34.2481667.9   -1.98544
GRID           4        -29.3555667.9   -1.70181
GRID           5        -24.4629667.9   -1.41817
GRID           6        -19.5703667.9   -1.13454
GRID           7        -14.6777667.9   -.850903
GRID           8        -9.78516667.9   -.567269
GRID           9        -4.89258667.9   -.283634
GRID          10        3.055e-13667   3.841e-11
GRID          11        4.892579667.9   .2836343
8ulbf1ek

8ulbf1ek2#

以下方案将:

  • 假设文件名为:grid_data.txt
  • 查找数字包围的连字符,例如3.841-11,并转换为3.841e-11
    *忽略数字开头的连字符,例如-44.0332

fix_float()函数允许进行转换。

  • 使用的正则表达式为\d(-)\d

代码:

import re

pattern = re.compile(r'\d(-)\d')

def fix_float(m):
    val = m.group(0).replace('-','e-')
    return val

def read_fem(location):  
    mesh = open(location, 'r').read().splitlines()
    point = []

    for m in mesh:
        if '$' not in m:
            if 'GRID' in m:
                number = int(m[8:16])
                x = float(pattern.sub(fix_float, m[24:32]))
                y = float(pattern.sub(fix_float, m[32:40]))
                z = float(pattern.sub(fix_float, m[40:48]))

                point.append([number, x, y, z])
    return point

# Extract the data from the file                
data = read_fem('grid_data.txt')

# Display the data
for point in data:
    print(point)

输出:

[1, -44.0332, 667.9, -2.55271]
[2, -39.1406, 667.9, -2.26907]
[3, -34.2481, 667.9, -1.98544]
[4, -29.3555, 667.9, -1.70181]
[5, -24.4629, 667.9, -1.41817]
[6, -19.5703, 667.9, -1.13454]
[7, -14.6777, 667.9, -0.850903]
[8, -9.78516, 667.9, -0.567269]
[9, -4.89258, 667.9, -0.283634]
[10, 3.055e-13, 667.9, 3.841e-11]
[11, 4.892579, 667.9, 0.2836343]

相关问题