简体   繁体   English

使用python替换字符串中的子字符串

[英]Replace substring in string using python

I have csv data 我有csv资料

url
vk.com/feed
vk.com/friends
vk.com/kobrinsky
vk.com/exclusive_muzic
vk.com/o_gordievskaya
vk.com/exclusive_muzic
vk.com/o_gordievskaya

And I need replace some substring. 我需要替换一些子字符串。

[u'o_gordievskaya', u'pavel__pechenkin', u'tima555102', u'bl2225554445']

to

[23183634, 86313977, 27313686, 3935697]

I try 我尝试

users = pd.read_excel('users.xlsx')
data = pd.read_csv('get_id.csv', error_bad_lines=False)
scrname = users['scrname']
id_scr = users['id']
urls = data['url']
for url in urls:
    for scr in scrname:
        if scr in url:
            url.replace(scr, id_scr)

I add column scrname and id to lists, I think it would be easier, but it doesn't help me 我将列的scrnameid添加到列表中,我认为这会更容易,但是对我没有帮助

You are using id_scr in the replace but you need to use the corresponding element 您在替换中使用id_scr ,但需要使用相应的元素

index = url.index(scr)    
url.replace(scr, id_scr[index])

or shorter : 或更短:

url.replace(scr, id_scr[url.index(scr)])

It doesn't work because in for url in urls: url is a copy of your data. 这不起作用,因为在for url in urls: url是数据的副本。

But since you use pandas, you can simply use replace 但由于您使用的是熊猫,因此只需使用replace

sub = {'o_gordievskaya' :23183634, 'pavel__pechenkin' : 86313977, ...}
data.replace(sub)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM