Home > other >  how to separate text lines in python
how to separate text lines in python

Time:11-02

I am trying to split diffrent lines of text in python. I tried to use the split function and it dosent work for what I want. here is an example. `

list = open(file,"r")
for Password in passlist:
       print(list)

`

and I want it to give this output: number 1 and number 2

CodePudding user response:

I do not know how you have saved the Passwords in your File but if you would like to open the passwords you could do something like this:

passlist = open("file" ,"r")
x = passlist.read().split(", ") <--- change to the format you use to save the passwords 
print(x)

then you could assign password1 and 2 as follows:

password1 = x[0]
password2 = x[1]

CodePudding user response:

If you want to have each line in a list:

# 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

Avoid using python keywords as variables as this could result in unnecessary problems. If you need to use a word similar to 'list', use something like 'lst' or '_list'. Same goes for any other keywords.

  • Related