[PYTHON] How to separate strings with','


When controlling the measuring instrument with a PC, when the measured values were acquired, most of the returned values were separated by ```','` ``. It becomes a character string. Consider how to separate the strings. Let's consider the expected return value below.

result = '3.14E-3, -128.96'

 By the way, this is the return of the measured value of the lock-in amplifier.

# Use slices
R = result[0:7]
Th = result[9:16]
print('R:{}, Th:{}'.format(R, Th))
# R:3.14E-3, Th:-128.96

It is possible to decompose the character string by slicing and output it. However, at this time, is it malfunctioning depending on the measuring instrument? Depending on the value, there may be a space in the return value. At that time, the program stops. It's a healthy PC

Use the split () function

result_list = result.split(',')
R = result_list[0]
Th = result_list[1]
print('R:{}, Th:{}'.format(R, Th))
# R:3.14E-3, Th:-128.96

' 3.14E-3, -128.96'Even for a return value like, this will separate it properly with a comma.

# Finally
 This time, I wrote about the method of processing the return value that is often seen in measuring instruments.
 To be honest, either method is OK, and I think it depends on your taste.
 However, since it sometimes malfunctions and the space key is inserted, and the header peculiar to the measuring instrument is attached, I think that it is necessary to take flexible measures such as using it properly or combining it.

Recommended Posts

How to separate strings with','
How to update with SQLAlchemy?
How to cast with Theano
How to Alter with SQLAlchemy?
How to RDP with Fedora31
How to Delete with SQLAlchemy?
How to cancel RT with tweepy
Python: How to use async with
How to deal with imbalanced data
How to install python-pip with ubuntu20.04LTS
How to deal with imbalanced data
How to get started with Scrapy
How to get started with Python
How to deal with DistributionNotFound errors
How to get started with Django
How to Data Augmentation with PyTorch
How to use FTP with Python
How to calculate date with python
How to install mysql-connector with pip3
How to INNER JOIN with SQLAlchemy
How to install Anaconda with pyenv
How to authenticate with Django Part 2
How to authenticate with Django Part 3
How to do arithmetic with Django template
[Blender] How to set shape_key with script
How to get parent id with sqlalchemy
How to add a package with PyCharm
How to install DLIB with 2020 / CUDA enabled
How to use ManyToManyField with Django's Admin
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
How to work with BigQuery in Python
How to use Ass / Alembic with HtoA
How to deal with enum compatibility errors
How to use Japanese with NLTK plot
How to do portmanteau test with python
How to search Google Drive with Google Colaboratory
How to display python Japanese with lolipop
How to download youtube videos with youtube-dl
How to use jupyter notebook with ABCI
"How to pass PATH" to learn with homebrew
How to scrape websites created with SPA
How to use CUT command (with sample)
How to enter Japanese with Python curses
[Python] How to deal with module errors
How to install zsh (with .zshrc customization)
How to read problem data with paiza
How to use SQLAlchemy / Connect with aiomysql
How to get started with laravel (Linux)
How to group volumes together with LVM
How to install python3 with docker centos
How to use JDBC driver with Redash
How to selectively delete past tweets with Tweepy
How to create sample CSV data with hypothesis
How to read a CSV file with Python 2/3
How to use GCP trace with open Telemetry
How to debug with Jupyter or iPython Notebook
How to send a message to LINE with curl
How to enjoy programming with Minecraft (Ruby, Python)
How to deal with errors when hitting pip ②
[REAPER] How to play with Reascript in Python