为什么Python会向一些列表元素添加换行符?

2024-04-27 13:45:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从一个名为WeaponShack.list的文本文件中读取文件

这些是“武器棚列表”的内容:

Grenade
Ak-47
Shotgun
Makarov
Minigun
Bat
Katana
Chainsaw
Flamethrower

没有空格。这些项目按原样复制粘贴

我试图去掉一些换行符,但在打印列表时,“\n”字符总是出现在某些元素上。我不明白为什么

我尝试使用.strip('\n'),但是“\n”字符仍然会在结尾弹出 一些元素

print("Taking weapons from Weapon shack")
weapons = []
with open("WeaponShack.list",'r') as ws:
    weapons = ws.readlines()
    for weapon in weapons:
        weapons.remove(weapon)
        weapon = weapon.strip('\n')
        weapons.append(weapon)
ws.close()
print(weapons)

预期输出为:

Taking weapons from Weapon shack ['Ak-47', 'Makarov', 'Bat', 'Chainsaw', 'Grenade', 'Minigun', 'Flamethrower', 'Katana', 'Shotgun']

但实际输出是:

Taking weapons from Weapon shack ['Ak-47\n', 'Makarov\n', 'Bat\n', 'Chainsaw\n', 'Grenade', 'Minigun', 'Flamethrower', 'Katana', 'Shotgun']

有人知道为什么会这样吗

编辑 我的重点是“\n”字符出现的原因。不是如何遍历列表。 同时尝试以下建议:adding a new list

print("Taking weapons from Weapon shack")
weapons = []
newWeaponList = []
with open("WeaponShack.list",'r') as ws:
    weapons = ws.readlines()
    for weapon in weapons:
        weapons.remove(weapon)
        weapon = weapon.strip('\n')
        newWeaponList.append(weapon)
ws.close()
print(newWeaponList)

生成此输出:

Taking weapons from Weapon shack ['Grenade', 'Shotgun', 'Minigun', 'Katana', 'Flamethrower']

奇怪,因为它没有显示所有的元素


Tags: fromwslistprintweaponshotgunkatanagrenade
2条回答

@rdas谢谢你的帮助

““ 在遍历列表时从列表中删除内容会扰乱列表中元素的相对顺序。由于只对列表进行一次迭代,因此可能会跳过某些元素。因此\n没有出现-只是从未从某些元素中删除它。“

我删除了weapons.remove(weapon)行,它修复了它

干杯

这是因为您正在读取和修改阻止遍历每个元素的列表。结尾带有'\n'的武器永远不会被weapon.strip('\n')触及

要使它更干净,请删除遍历问题并除去新的空间,请尝试以下操作answer:

print("Taking weapons from Weapon shack")
with open("weapons.list") as ws:
    readWeapons = ws.readlines()
    for weapon in readWeapons:
        weapon = weapon.rstrip('\n')
        weapons.append(weapon)
print(weapons)

编辑:

为了回答您的澄清,这是因为python中for循环是如何工作的。我不知道确切的细节,但似乎for循环实际上实时访问了数组,而不是一次读取整个数组。查看test.list中的读数,该列表包含与A-D对应的四行:

print("Taking item from test\n")
with open("test.list",'r') as ws:
    elements = ws.readlines()
    print(elements)
    print()
    for element in elements:
        print('Current element: ' + element)
        elements.remove(element)
        print('Element pre-strip: ' + str(elements))
        element = element.strip('\n')
        print('Stripped element: ' + element)
        elements.append(element)
        print('Element list post-strip: ' + str(elements))

输出:

Taking item from test

['A\n', 'B\n', 'C\n', 'D\n']

Current element: A

Element pre-strip: ['B\n', 'C\n', 'D\n']
Stripped element: A
Element list post-strip: ['B\n', 'C\n', 'D\n', 'A']
Current element: C

Element pre-strip: ['B\n', 'D\n', 'A']
Stripped element: C
Element list post-strip: ['B\n', 'D\n', 'A', 'C']
Current element: A
Element pre-strip: ['B\n', 'D\n', 'C']
Stripped element: A
Element list post-strip: ['B\n', 'D\n', 'C', 'A']
Current element: A
Element pre-strip: ['B\n', 'D\n', 'C']
Stripped element: A
Element list post-strip: ['B\n', 'D\n', 'C', 'A']

由于附加而不是前置,当for循环从数组的索引a移动到数组的索引a+1时,将跳过其他所有元素。这导致B和D(索引1和3)被跳过,A被读取3次

相关问题 更多 >