python - 问题将 IP 地址字符串从 for 循环附加到列表

标签 python list dictionary user-input

我正在尝试创建一个脚本来获取主机名和 IP 地址并将它们写入一个列表,我可以将其压缩到一个单独的字典中。除了我在变量中没有任何数字我只是得到“.”这一事实之外,它还在工作。部分而不是数量。不太确定我做错了什么,因为这之前没有发生在我身上......看看:

HOST_NAME = []
IP_ADDRESS = []

ADDITION_NAME = "Please enter a word or two explaining the addition (used for file name): "

ENTRY_AMOUNT = int(input("How many hosts will need records? "))

for number in range(ENTRY_AMOUNT):
    hostname = raw_input("What is the hostname of the host: ")
    address = raw_input("What is the IP address of the host: ")
    HOST_NAME.append(hostname)
    IP_ADDRESS.append(IP_ADDRESS)


A_RECORD_ENTRY = dict(zip(HOST_NAME,IP_ADDRESS))


print HOST_NAME # test for correct appendages
print IP_ADDRESS # test for correct appendages
print A_RECORD_ENTRY # testing code for dictionary output

这给了我以下输出:

C:\Users\fallacy>a_record_add.py
How many hosts will need records? 1
What is the hostname of the host: test
What is the IP address of the host: 192.168.1.1
['test']
[[...]]
{'test': [[...]]}

它只是添加了如上所述的点,我以前没有遇到过这个,所以请让我知道我做错了什么!非常感谢!

最佳答案

IP_ADDRESS.append(IP_ADDRESS)

我认为你的意思是写:

IP_ADDRESS.append(address)

关于python - 问题将 IP 地址字符串从 for 循环附加到列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36266533/

相关文章:

python - 从 Mega.nz 文件中抓取文本 (Python)

python - 在 Python 中将序列拆分为单个步骤的最优雅方法

python - 在 Python 字典中存储和更新列表 : why does this happen?

ios - 从核心数据读取时获取属性名称和值

python - 如何让 App Engine SDK 跟踪通过 sys.path 添加的目录中的文件更改?

python - 将两个 pandas DataFrame 与一个唯一的列组合起来,并保留行索引 (Python)

C++ 列表迭代器无法初始化

python - 星球大战 api => IndexError : list index out of range error

java - 列表与 map /字典

python - 从文本文件创建字典