简体   繁体   中英

How to check the data in the csv file is a string or number or alphanumeric in python?

I am learning data analysis using Python. I am successfully able to read the data and print the data of a csv file using Python. My mentor gave me a task to find out whether the data that is displaying is

  • a string,
  • a number, or
  • alphanumeric.

To check whether a variable v is a string, or number, you may look at the following:

>>> v = 'test'
>>> isinstance(v, str)
True

Or

>>> t = 2
>>> isinstance(p, int)
True

Now if it is a string and you want to check more precisely the content of that string (alpha, alphanumerical, digit, etc.) you can use the built-in python methods:

Now try:

>>> v.isalnum()
True
>>> v.isalpha()
True
>>> v.isdigit()
False
>>> v.islower()
False
FileName['ColumnName'][0].isdigit()

This will Check whether the First Value of the Specified Column is Digit or Not. From this, we can easily infer if it's a digit or not(Can even check for strings also)

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