简体   繁体   中英

Replace nan with empty string in python

My requirement: I want to replace nan with "" (empty string)

Example

data = """{"name":"siva","id":"111","grade":"12","job_id":nan,"location":"ananb"}"""

If you see above example, i just want to replace nan (Not a Number) not the string consists of nan(ananb)

Can any one suggest a solution for this?

A non generic solution for your specific example could be

data.replace(":nan",":")

This wouldn't replace actual values of the dictionary

You can use the string.replace method.

data.replace("nan","")

Hope this solves your problem.

更好的方法是使用正则表达式 ike 替换任何没有被其他字母包围的nan匹配项:

import re

print(re.sub("\\Wnan\\W", "", data))

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