Home > other >  How do I stop it from returning "\n" along with data in the list?
How do I stop it from returning "\n" along with data in the list?

Time:12-15

def register(string):
  userpass = string.split(" ")
  k7 = userpass[0]
  k2 = userpass[1]
  users.append[k7]
  passes.append[k2]
  return "Registered!"

don't worry about this part ^^^ it's just an unused function that I'll use soon.

users = []
passes = []

csvfile = open('upassword.csv', 'r', encoding= 'utf-8')
for i in csvfile:
  x = i.split(',')
  if x[0] != 'username':
    print('username: ', x[0])
    users.append(x[0])
  if x[1] != 'password':
    print('password:', x[1])
    passes.append(x[1])

So here, I am printing the list just to test out how the values come out from the csv file and when I append and print them to some lists in the code, the first password returns with '\n' at the end of its data string which I can't remove for some reason. How do I remove that '\n' part?

print(users, passes)

CodePudding user response:

You can use strip to remove unnecessary spaces \ns and \ts:

users = []
passes = []

csvfile = open('upassword.csv', 'r', encoding= 'utf-8')
for i in csvfile:
  i = i.strip()
  x = i.split(',')
  if x[0] != 'username':
    print('username: ', x[0])
    users.append(x[0])
  if x[1] != 'password':
    print('password:', x[1])
    passes.append(x[1])
  • Related