python - python中的唯一排序

标签 python string

我是 python 的新手。我想从 file.txt 中获取唯一的字符串 我有一些这样的数据......

Tempranillo     Rioja_%28wine%29%23Wine_regions
Gr%C3%BCner_Veltliner       Czech_Republic_%28wine%29
Marsanne        California_%28wine%29
Carm%C3%A9n%C3%A8re     Wines_of_Chile
Carm%C3%A9n%C3%A8re     Washington_%28U.S._state%29
Gr%C3%BCner_Veltliner       Czech_Republic_%28wine%29

所以,我尝试了以下代码:

import re
import string
import urllib

for line in open('file.txt', 'r').readlines():
    left, right = string.split(line)
    relation = string.split(line)


    dom = relation[0]
    rang = relation[1]

    dom = urllib.unquote(relation[0])
    dom = dom.replace('_', ' ')


    rang= urllib.unquote(relation[1])
    rang = rang.replace('_', ' ')

如何进一步进行。我需要以这种格式获得 (dom rang) 的唯一共现:

Tempranillo     Rioja (wine) Wine regions
Marsanne        California (wine)

任何形式的帮助将不胜感激。谢谢!

最佳答案

要从文件中过滤掉重复的行,请执行以下操作:

with open("file.txt") as f:
    unique_lines = set(f)

关于python - python中的唯一排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6557936/

相关文章:

python - 在 numpy 中减去数组并用 pylab 绘图

Python 记录多个文件

python - 用 Python 构建一个最小的插件架构

java - 格式字符串变为 xxx1、xx10 或 1###、10## 等

python - 如何通过python在选定的Gmail邮箱中存档电子邮件

python - 在 for 循环中将数据帧 append 在一起

ruby - 为什么在 Ruby 中有一个 String#replace?

python - int 到字符串连接

php - 在字符串中选择从位置到行尾

java - 在java中提取已知模式的子串