简体   繁体   English

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

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

Hello, I recently started to learn Python, so that's my best explain to you, cause my English skills are not Perfectly. 您好,我最近开始学习Python,因此,这是我最好的向您解释的原因,因为我的英语水平不是很完美。 I made a script which is reading a list from text file, and then my problem is converting it to string, so I could display it in the print function. 我制作了一个脚本,该脚本正在从文本文件中读取列表,然后我的问题是将其转换为字符串,因此可以在打印功能中显示它。 After doing that, when user is typing his "Nickname", lets say. 完成之后,当用户键入其“昵称”时,可以说。 The script is already readen the list from text file. 该脚本已从文本文件中读取列表。 Also the i don't know if used the split(',') Function, that should split the words with those , from the words in the text file used for list. 另外,我不知道是否使用split(',')函数,该函数应该将这些字符与用于列表的文本文件中的单词分开。 Here are some pictures of my code. 这是我的代码的一些图片。 https://gyazo.com/db797ca0998286248bf846ac70c94067 (Main code) https://gyazo.com/918aaba9b749116d842fccb78f6204a8 (Text file - list of usernames which are "BANNED") The text code file name is Listas_BAN.txt. https://gyazo.com/db797ca0998286248bf846ac70c94067 (主代码) https://gyazo.com/918aaba9b749116d842fccb78f6204a8 (文本文件-用户名“被禁止”列表)文本代码文件名为Listas_BAN.txt。

I've tried to do all this thing myself, i did some research before posting this, but many methods are outdated. 我已经尝试自己做所有这件事,在发布之前我做了一些研究,但是很多方法已经过时了。

# 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 here enter image description here 在此处 输入图像描述在 此处 输入图像描述

I'm getting Wrong Syntax Error 我收到错误的语法错误

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))

EDIT: 编辑:

plus, you shall consider using string formatting: 另外,您应考虑使用字符串格式:

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

or python 3.7 或python 3.7

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

EDIT: 编辑:

f.readlines()

https://docs.python.org/3.7/tutorial/inputoutput.html 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(). 如果要读取列表中文件的所有行,则还可以使用list(f)或f.readlines()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM