从文本文件读取列表并将其转换为字符串

2024-04-16 18:54:22 发布

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

您好,我最近开始学习Python,这是我最好的解释,因为我的英语水平不太好。 我编写了一个脚本,从文本文件中读取一个列表,然后我的问题是将它转换为字符串,这样我就可以在print函数中显示它。这样做之后,当用户输入他的“昵称”时,让我们说。脚本已从文本文件中读取列表。另外,我不知道是否使用了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

enter image description hereenter image description here

语法错误


Tags: the代码nametxt脚本列表read单词
1条回答
网友
1楼 · 发布于 2024-04-16 18:54:22
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

If you want to read all the lines of a file in a list you can also use list(f) or f.readlines().

相关问题 更多 >