python - 删除Python中第一个实例后的字符串字符

标签 python string replace

我认为这应该很简单,但现在是星期五下午,我的大脑不太清楚。

我正在编写一个小文件解析,下面的代码将一组字符串转换为数据帧,并将字符串拆分。

以下是一些示例字符串:

1. NC_002523_1  Serratia entomophila plasmid pADAP, complete sequence.

2. NZ_CM003366_0    Pantoea ananatis strain CFH 7-1 plasmid CFH1-7plasmid2, whole genome shotgun sequence.

3. NZ_CP014491_0    Escherichia coli strain G749 plasmid pG749_3, complete sequence.

4. NC_015062_0  Rahnella sp. Y9602 plasmid pRAHAQ01, complete sequence.

我没有预料到第四个条目中 sp 之后的 .,正如您在下面的代码中看到的,我在 . 获取排名的第一个整数。因此,我收到一个 ValueError,即列数超出预期。

# Define the column headers for the section since the file's are too verbose and ambiguous
SigHit.Columns = ["Rank", "ID", "Description"]

# Store the table of loci and associated data (tab separated, removing last blank column.

# Use StringIO object to imitate a file, which means that we can use read_table and have the dtypes
# assigned automatically (necessary for functions like min() to work correctly on integers)

SigHit.Table = pd.read_table(
               io.StringIO(u'\n'.join([row.rstrip('.') for row in sighits_section])),
               sep='\.|\t',
               engine='python',
               names=SigHit.Columns)

我能想到的最简单的解决方案(直到其他一些边缘情况破坏它)是替换除第一次出现之外的每个 . 。如何做到这一点?

我看到有一个maxreplace argument.replace,但这会与我想要的相反,并且只会替换第一个实例。

有什么建议吗? (更强大的解析方法也是一个有效的选择,但我需要更改的代码越少越好)。

最佳答案

使用正向后查找来确保点前面有一个数字 - sep='(?<=\d)\.|\t'

例如:

import pandas as pd
import io

columns = ["Rank", "ID", "Description"]

sighits_section = '''1. NC_002523_1\tSerratia entomophila plasmid pADAP, complete sequence.
2. NZ_CM003366_0\tPantoea ananatis strain CFH 7-1 plasmid CFH1-7plasmid2, whole genome shotgun sequence.
3. NZ_CP014491_0\tEscherichia coli strain G749 plasmid pG749_3, complete sequence.
4. NC_015062_0\tRahnella sp. Y9602 plasmid pRAHAQ01, complete sequence.'''.splitlines()

tab = pd.read_table(io.StringIO(u'\n'.join([row.rstrip('.') for row in sighits_section])),
                    sep='(?<=\d)\.|\t',
                    engine='python',
                    names=columns)

print(tab)

打印

   Rank              ID                                        Description
0     1     NC_002523_1  Serratia entomophila plasmid pADAP, complete s...
1     2   NZ_CM003366_0  Pantoea ananatis strain CFH 7-1 plasmid CFH1-7...
2     3   NZ_CP014491_0  Escherichia coli strain G749 plasmid pG749_3, ...
3     4     NC_015062_0  Rahnella sp. Y9602 plasmid pRAHAQ01, complete ...

为了更加安全,您可能需要在点旁边添加空格作为分隔符 - sep='(?<=\d)\.\s|\t' - 缓解,如果你有,例如10.1在你的描述中的某个地方。这无论如何都不是防弹的。

更安全 - 当您一次处理一行数据时,您可以添加一个断言,即该数字也是字符串中的第一个字符 sep='(?<=^\d)\.\s|\t' 。但是,这会在数字高于 10 时崩溃。

关于python - 删除Python中第一个实例后的字符串字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49819880/

相关文章:

javascript - 从字符串中删除最后一个字符实例 - Javascript - 重访

python - 确定多维numpy数组中是否至少有一个零

Javascript:如果冒号则删除最后一个字符

python - 如何在 python pandas 中标记循环数的值

ruby - 在 Ruby 中,将 "chomp"放在字符串开头而不是结尾的最简单方法是什么?

c++ - 如何合并两个 LPCWSTR?

c++ - 在有序的 STL 容器中查找以前缀开头的所有字符串(非低位 ASCII)

python - 快速替换 numpy 数组中的值

python - 如何将单个 parquet 文件从 s3 读取到 dask 数据帧中?

python - python 中的动态语句在一个实体发生变化时绘制图形