简体   繁体   中英

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

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

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

EDIT:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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