设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 6936|回复: 1
打印 上一主题 下一主题

How to delete the ½ in Python

[复制链接]

2

主题

0

好友

86

积分

注册会员

Rank: 2

跳转到指定楼层
楼主
发表于 2016-11-2 03:43:15 |显示全部楼层 |倒序浏览
I need to run one SQL file, but i have a below sentance in my SQL fiel: 13½ Elm Street. This ½ makes my SQL failed.
So i need to write the Python code to get rid of the symbol ½, my goal is to convert it to:      13 Elm Street.
the firs part of script that i wrote is :
                import os
                with open(r'D:\mysql_10.sql', 'r') as infile
                data = infile.read()
                data = data.replace('13½', "13")
                    data = data.replace("13½ Elm Street", "13 Elm Street")
                -----
Both of above replacement do not work, so i am wondering what's wrong with that. How could I delete that ½? Please help me to solve that problem.  

Thanks a lot
回复

使用道具 举报

2

主题

0

好友

86

积分

注册会员

Rank: 2

沙发
发表于 2016-11-2 20:53:30 |显示全部楼层
crossin先生 发表于 2016-11-2 16:17
Two possible reasons
1. You read file, replaced ½, but not save it back or to another file.
2. ½ i ...

你好,crossin先生,我是python菜鸟,有点没明白把那个utf8加到哪,能帮忙改下吗?以下是我代码,谢谢啦。
import os
with open(r'D:\OA\OAText\mysql_10.sql', 'r') as infile,open(r'D:\OA\OAText\mysql_N10.sql', 'w') as outfile:
    data = infile.read()
    data = data.replace("13½", "13")
    data = data.replace("½", "")
    data = data.replace("13½ Elm Street", "13 Elm Street")
   
    outfile.write(data)
    infile.close()
    outfile.close()
    os.remove('D:\OA\OAText\mysql_10.sql')
    os.rename(r'D:\OA\OAText\mysql_N10.sql',r'D:\OA\OAText\mysql_10.sql')
万分感谢啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-20 09:49 , Processed in 0.016408 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部