python - 消除python脚本中最后一个字符串后的文本

标签 python dictionary

从表文件创建字典,其中某些列有特殊字符,如“|”但我需要删除最后一个“|”之后的所有文本。

例如:

A     this_is|my_A|best|result|      20
B     this_is|my_B|best|result|mess  40
C     this_is|my_C|best|result|me..  32

我写这个是为了创建字典:

for line in file:
        query = line.strip().split('\t')[0]
        data =  line.strip().split('\t')[1:2]
        subject = line.strip().split('\t')[1]
        if query not in best_hit:
                best_hit[subject] = data

导致像这样的乱七八糟的字典:

d = {'A': 'this_is|my_A|best|result|, 20' ,'B': 'this_is|my_B|best|result|mess  40', 'C':'this_is|my_C|best|result|me..  32' }

我的目的是在将其包含在字典中之前删除“mess”和“me..”文本,因为我需要这个值来与没有这些文本的其他列表进行比较。

A     this_is|my_A|best|result|      20
B     this_is|my_B|best|result|      40
C     this_is|my_C|best|result|      32

我自己的解决方案:

old_result = line.strip().split('\t')[1]
new_result = old_result.split('|')
subject = new_result[0]+'|'+new_result[1]+'|'+new_result[2]+'|'+new_result[3]+'|

最佳答案

拆分可能不符合性能,但算法更简单。

source = """

A   this_is|my_A|best|result|   20
B   this_is|my_B|best|result|mess   40
C   this_is|my_C|best|result|me..   32

"""

source = source.strip()
source = source.split('\n')

result = {}

for i in xrange(len(source)):
    asplit = source[i].split('\t')
    bsplit = asplit[1].split('|')
    bsplit[-1] = ''
    asplit[1] = '|'.join(bsplit)

    if not asplit[0] in result:
        result[asplit[0]] = asplit[1] + '\t' + asplit[2]

print result

关于python - 消除python脚本中最后一个字符串后的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40910321/

相关文章:

具有多个类和 Tkinter 的 Python 属性错误

python - 如何防止Python中的netcdf4在使用日期时间时加载整个变量

python - 使用并行处理计算 2D 中每对点之间的距离

python - 如何将 Tavern 测试的响应保存在 JSON 文件中?

Clojure 映射-最长

python - 有没有办法通过将 unicode 字符串转换为其他编码来打印出由 unicode 字符串组成的字典?

python - 在Python中使用不同的键并发写入字典

c++ - 将 C++ OpenCV 代码包装为 Python

python - 如何自动获取本地时区的时区偏移量?

python - 创建字典 Python 的字典