简体   繁体   中英

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. I then imported the file into python (using the pandas package and turning the columns into arrays) but I'm facing a problem. 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:

数据的打印屏幕

Is there any way I can "teach" python to read my data? 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.

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

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