遍历Python列表

时间:2018-11-01 21:59:33

标签: python

我有一个列表,我需要从每个项目中去除\ n使其与另一个列表匹配。有没有比执行循环脚本更简单的方法?并必须附加到全新列表中?

special_chars = {"%20":"    ",
"%21":"!",
"%22":'"',
"%23":"#",
"%24":"$",
"%25":"%",
"%26":"&",
"%27":"'",
"%28":"(",
"%29":")",
"%2A":"*",
"%2B":"+",
"%2C":",",
"%2D":"-",
'%2E':".",
'%2F':"/",
'%3A':":",
'%3B':";",
'%3C':"<",
'%3D':"=",
'%3E':">",
'%3F':"?",
'%40':"@"}

file = "file.txt"
file_open  = open(file)
readlines = file_open.readlines()
parameter = []

for key in special_chars:
    for line in readlines:
        if key in line:
           replace_value = key

replaced_value = special_chars[replace_value]

for line in readlines:
    parameter.append(line.replace(replace_value, replaced_value))

for line in parameter:
  print line.rstrip("\n") ### This is the part that I am having issue s with 

1 个答案:

答案 0 :(得分:0)

您可以做类似

的操作
list(map(lambda x: x.strip('\n')))