修改字符串 python

modify a string python

我有一个结构如下的 csv 文件:

num  mut
36    L
45    P
  ...

其中num表示突变的位置,mut表示突变。我必须在 num 位置用字母 mut 修改一个字符串。我在python中写了下面的代码:

import pandas as pd
import os
df = pd.read_csv(r'file.csv')
df_tmp=df.astype(str)
df_tmp["folder"]=df_tmp["num"]+df_tmp["mut"] #add a third column
f = open("sequence.txt", 'r')
content = f.read()
for i in range(len(df)):
     num=df_tmp.num.loc[[i]]-13
     num=num.astype(int)
     prev=num-1
     prev=prev.astype(int)
     mut=df_tmp.mut.loc[[i]]
     mut=mut.astype(str)
     new="".join((content[:prev],mut,content[num:])) #this should modify the file

但它returns我

TypeError: slice indices must be integers or None or have an __index__ method

我该如何解决?

编辑:也许更清楚我想做什么。我只需要在我的序列中插入第一个突变,将它保存到一个文件中,将文件复制到一个名为第三列(我在代码中添加)的文件夹中,用第二个突变做同样的事情,然后第三个等等。但是我一次只能插入一个突变。

多个突变:

IIUC,你最好 pandas,将你的数据框转换为字典,迭代并加入:

# input DataFrame
df = pd.DataFrame({'num': [36, 45], 'mut': ['L', 'P']})

# input string
string = '-'*50
# '--------------------------------------------------'

# get the positions to modify
pos = df.set_index('num')['mut'].to_dict()
# {36: 'L', 45: 'P'}

# iterate over the string, replace hte characters if in the dictionary
# NB. define start=1 if you want the first position to be 1
new_string = ''.join([pos.get(i, c) for i,c in enumerate(string, start=0)])
# '------------------------------------L--------P----'

单个突变:

string = '-'*50
# '--------------------------------------------------'

for idx, r in df.iterrows():
    new_string = string[:r['num']-1]+r['mut']+string[r['num']:]
    # or
    # new_string = ''.join([string[:r['num']-1], r['mut'], string[r['num']:]])
    
    with open(f'file_{idx}.txt', 'w') as f:
        f.write(new_string)

输出:

file_0.txt
-----------------------------------L--------------

file_1.txt
--------------------------------------------P-----

我用示例 file.csv 和一个空的 sequence.txt 文件尝试了您的代码,

在 for 循环的第一行代码中

num=df_tmp.num.loc[[i]]-13
#gives an error since the num in that location is str, to correct that:

num=df_tmp.num.loc[[i]].astype(int)-13 
# I used astype to convert it into int first

在此之后的下一个错误是在最后一行,切片索引类型错误, 这是因为,您用来切片的结果 prev 和 num content 变量不是 int,要获取 int 值,请向其添加 [0] 这样:

content="".join((content[:prev[0]],mut,content[num[0]:]))

现在应该不会出错了