我正在嘗試在 python 中拆分不同的文本行。我嘗試使用 split 功能,但它并不適合我想要的。這是一個例子。`
list = open(file,"r")
for Password in passlist:
print(list)
`
我希望它給出這個輸出:
number 1
和
number 2
uj5u.com熱心網友回復:
我不知道您是如何將密碼保存在檔案中的,但如果您想打開密碼,您可以執行以下操作:
passlist = open("file" ,"r")
x = passlist.read().split(", ") <--- change to the format you use to save the passwords
print(x)
那么您可以按如下方式分配密碼1和2:
password1 = x[0]
password2 = x[1]
uj5u.com熱心網友回復:
我真的不明白你的意思,但我猜你想要這樣?
密碼 1
密碼 2
如果是這樣,那么你可以使用這個:
With open("file", "r") as f:
contents=f.read()
print(contents)
uj5u.com熱心網友回復:
如果您想將每一行放在串列中:
# Open the file (in read mode)
file = open(file, "r")
# Read the file and split at every new line
# The 'split()' function will return a list
content = file.read().split("\n")
# Close the file
file.close()
# Now each line should be in the list
print(content)
# Save the two variables by unpacking the list
num1, num2 = content
避免使用 python 關鍵字作為變數,因為這可能會導致不必要的問題。如果您需要使用類似于“list”的詞,請使用“lst”或“_list”之類的詞。任何其他關鍵字也是如此。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/527078.html
標籤:Python列表文本
上一篇:如何從字串中洗掉所有第一篇文章?
