将字符串转换为 int()

发布于 2024-11-16 00:51:22 字数 548 浏览 2 评论 0原文

我有一个如下所示的数据集:

0 _ _ 23.0186E-03  
10 _ _51.283E-03  
20 _ _125.573E-03

其中数字逐行排列(下划线代表空格)。

右侧列中的数字当前是该行字符串的一部分。我正在尝试将右侧的数字转换为数值(0.0230186 等)。一旦它们是简单的数字形式,我就可以用 int() 转换它们,但我需要更改“E”才能到达那里。如果您知道如何更改 E 的任何值(例如 E-01、E-22),那将会非常有帮助。

目前我的代码如下所示:

fin = open( 'stringtest1.txt', "r" )  
fout = open("stringtest2.txt", "w")

while 1:  
    x=fin.readline()

    a=x[5:-1]  
    ##conversion code should go here

    if not x:
        break

    fin.close()  
    fout.close()

I have a dataset that looks like this:

0 _ _ 23.0186E-03  
10 _ _51.283E-03  
20 _ _125.573E-03

where the numbers are lined up line by line (the underscores represent spaces).

The numbers in the right hand column are currently part of the line's string. I am trying to convert the numbers on the right into numerical values (0.0230186 etc). I can convert them with int() once they are in a simple numerical form, but I need to change the "E"s to get there. If you know how to change it for any value of E such as E-01, E-22 it would be very helpful.

Currently my code looks like so:

fin = open( 'stringtest1.txt', "r" )  
fout = open("stringtest2.txt", "w")

while 1:  
    x=fin.readline()

    a=x[5:-1]  
    ##conversion code should go here

    if not x:
        break

    fin.close()  
    fout.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一紙繁鸢 2024-11-23 00:51:22

我建议进行以下转换:

float(x.split()[-1])

str.split()将在空白处分割,并且 float() 会将字符串转换为数字,例如:

>>> '20  125.573E-03'.split()
['20', '125.573E-03']
>>> float('20  125.573E-03'.split()[-1])
0.12557299999999999

I would suggest the following for the conversion:

float(x.split()[-1])

str.split() will split on white space when no arguments are provided, and float() will convert the string into a number, for example:

>>> '20  125.573E-03'.split()
['20', '125.573E-03']
>>> float('20  125.573E-03'.split()[-1])
0.12557299999999999
凶凌 2024-11-23 00:51:22

您应该使用上下文处理程序,并且文件句柄是可迭代的:

with open('test1.txt') as fhi, open('test2.txt', 'w') as fho:
  for line in fhi:
    f = float(line.split()[-1])

    fho.write(str(f))

You should use context handlers, and file handles are iterable:

with open('test1.txt') as fhi, open('test2.txt', 'w') as fho:
  for line in fhi:
    f = float(line.split()[-1])

    fho.write(str(f))
我恋#小黄人 2024-11-23 00:51:22

如果我理解你想要正确执行的操作,则无需对 E 执行任何操作: in python float('23.0186E-03') returns 0.0230186,我认为这就是你想要的。

您所需要的只是:

fout = open("stringtest2.txt", "w")
for line in open('stringtest1.txt', "r"):
    x = float(line.strip().split()[1])
    fout.write("%f\n"%x)
fout.close()

在输出字符串中使用 %f 将确保输出采用十进制表示法(无 E)。如果您只使用 str(x),根据原始值,您可能会在输出中得到 E,因此正确的转换方法取决于您想要的输出:

>>> str(float('23.0186E-06'))
'2.30186e-05'
>>> "%f"%float('23.0186E-06')
'0.000023'
>>> "%.10f"%float('23.0186E-06')
'0.0000230186'

您可以将任何数字添加到 %f 来指定精度。有关使用 % 进行字符串格式化的更多信息,请参阅 http://rgruet.free.fr/ PQR26/PQR2.6.html#stringMethods(向下滚动到“使用 % 运算符进行字符串格式化”部分)。

If I understand what you want to do correctly, there's no need to do anything with the E's: in python float('23.0186E-03') returns 0.0230186, which I think is what you want.

All you need is:

fout = open("stringtest2.txt", "w")
for line in open('stringtest1.txt', "r"):
    x = float(line.strip().split()[1])
    fout.write("%f\n"%x)
fout.close()

Using %f in the output string will make sure the output will be in decimal notation (no E's). If you just use str(x), you may get E's in the output depending on the original value, so the correct conversion method depends on which output you want:

>>> str(float('23.0186E-06'))
'2.30186e-05'
>>> "%f"%float('23.0186E-06')
'0.000023'
>>> "%.10f"%float('23.0186E-06')
'0.0000230186'

You can add any number to %f to specify the precision. For more about string formatting with %, see http://rgruet.free.fr/PQR26/PQR2.6.html#stringMethods (scroll down to the "String formatting with the % operator" section).

鸠书 2024-11-23 00:51:22
float("20 _ _125.573E-03".split()[-1].strip("_"))
float("20 _ _125.573E-03".split()[-1].strip("_"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文