簡體   English   中英

從文本文件中讀取列表並將其轉換為字符串

[英]Reading list from text file and converting it to string

您好,我最近開始學習Python,因此,這是我最好的向您解釋的原因,因為我的英語水平不是很完美。 我制作了一個腳本,該腳本正在從文本文件中讀取列表,然后我的問題是將其轉換為字符串,因此可以在打印功能中顯示它。 完成之后,當用戶鍵入其“昵稱”時,可以說。 該腳本已從文本文件中讀取列表。 另外,我不知道是否使用split(',')函數,該函數應該將這些字符與用於列表的文本文件中的單詞分開。 這是我的代碼的一些圖片。 https://gyazo.com/db797ca0998286248bf846ac70c94067 (主代碼) https://gyazo.com/918aaba9b749116d842fccb78f6204a8 (文本文件-用戶名“被禁止”列表)文本代碼文件名為Listas_BAN.txt。

我已經嘗試自己做所有這件事,在發布之前我做了一些研究,但是很多方法已經過時了。

# Name
name = input("~ Please enter Your name below\n")
print("Welcome " + str(name))

def clear(): return os.system('cls')

clear()  # Clearina viska.

# define empty list
Ban_Listo_Read = open('Listas_BAN.txt', mode='r')
Ban_Listo_Read = Ban_Listo_Read.readlines()
Ban = Ban_Listo_Read.list(ban)
# Print the function (LIST) in string .
print("Your'e Banned. You'r nickname is - ", + Ban_Listo_Read).Select (' %s ', %s str(name)) # Select the User nickname from 
                                                                                                                                # The input which he typed. (Check for BAN, In the List.)
                                                                                                                                # Text file is the List Location . - Listas_BAN.txt

在此處 輸入圖像描述在 此處 輸入圖像描述

我收到錯誤的語法錯誤

ll = open('untitled.txt', mode='r').readlines()
print("".join(ll).replace('\n', '.'))

name = input("~ Please enter Your name below\n")

if name in ll:
    print('your name {n} is in the list'.format(n=name))

編輯:

另外,您應考慮使用字符串格式:

var1 = ...
var2 = ...
print("{x}...{y}".format(x=var1, y=var2)

或python 3.7

print(f"{var1}...{var2}")

編輯:

f.readlines()

https://docs.python.org/3.7/tutorial/inputoutput.html

如果要讀取列表中文件的所有行,則還可以使用list(f)或f.readlines()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM