简体   繁体   中英

Python Scientific Notation Format

I have a large set of data with small values in the order of 10^-3. However they're written without the "e"; for example, 9.993e-3 is written as 9.993-3 in the file. This creates problems in numpy. How can I add that "e" in between the last digit and the negative sign?

Some notes: The data set is contained in a file with no delimiters (number occupies a fixed width) and some of the data aren't necessarily in the order of 10^-3.

Small snippet of data:

11.752811.950003-6.973-3
11.772591.950003 -.00636
11.792371.950002-5.746-3
s='9.993-3'
s1=s[:s.index('-')] + 'e' + s[s.index('-'):]
print(s1)

When you reference the data you can just use:

str.replace("-", "e-")

Or if you have some negative data values use regex:

import re
for line in some_file:
    line = re.sub(r'(?<=[1-9]{1,})(-)', 'e-', line)

Here's a solution for what I understand the format of your data to be. Please confirm exact format if I've misunderstood.

data = '11.752811.950003-6.973-311.772591.950003 -.0063611.792371.950002-5.746-3' 
fixed_width = 8

numbers = []
for i in range(0, len(data), fixed_width):
    num = data[i:i+fixed_width]
    num = float(num[:2] + num[2:].replace('-', 'E-'))
    numbers.append(num)
import re

data = '11.752811.950003-6.973-311.772591.950003 -.0063611.792371.950002-5.746-3'  

1. create list of 8-digit numbers  

dataList = re.findall(r'.{8}', data)  

2. add e to the numbers that need it   

>>>[re.sub(r'(-\d)$', r'e\1',number) for number in dataList]

['11.75281', '1.950003', '-6.973e-3', '11.77259', '1.950003', ' -.00636', '11.79237', '1.950002', '-5.746e-3']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM