简体   繁体   English

从.txt文件中读取arrays作为数字而不是字符串

[英]Reading arrays from .txt file as numbers instead of strings

I'm using an automatic data acquisition software that exports the data as.txt files.我正在使用将数据导出为 .txt 文件的自动数据采集软件。 I then imported the file into python (using the pandas package and turning the columns into arrays) but I'm facing a problem.然后我将文件导入 python (使用 pandas package 并将列转换为数组),但我遇到了问题。 Python can't "read" the data because the automatic data acquisition software exported it into the following number format, and so Python is treating each entry of the array as a string instead of a number: Python 无法“读取”数据,因为自动数据采集软件将其导出为以下数字格式,因此 Python 将数组的每个条目视为字符串而不是数字:

数据的打印屏幕

Is there any way I can "teach" python to read my data?有什么办法可以“教” python 读取我的数据? Or to automatically rewrite the entries in the array so they're read as numbers?或者自动重写数组中的条目以便将它们读取为数字?

You can simply change the comma in the strings with a dot and use float() to parse it.您可以简单地用点更改字符串中的逗号并使用float()来解析它。

number = float('7,025985E-36'.replace(',', '.'))

print(number)
print(type(number))

The above code would print:上面的代码将打印:

7.025985e-36
<class 'float'>

You can try this way.你可以试试这个方法。

>>> value=str('7,025985e-36')
>>> value2=value.replace(',', '.')
>>> float(value2)
7.025985e-36

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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