-3

After parsing a csv file, i got a list like this:

['name', 'AGATC', 'AATG', 'TATC\n']

Is there any way that I can delete the newline (\n) character from the last element of the list so that the length of the element won't include the newline? I have tried the built-in replace() function but it did not work.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
WillBui
  • 5
  • 1
  • `\n` is not a null character. It's a newline character. – gspr Feb 09 '21 at 08:32
  • 2
    Does this answer your question? [How can I remove a trailing newline?](https://stackoverflow.com/questions/275018/how-can-i-remove-a-trailing-newline) – dpwr Feb 09 '21 at 08:34
  • Does this answer your question? [Remove new line \n reading from CSV](https://stackoverflow.com/questions/27175874/remove-new-line-n-reading-from-csv) – Tomerikoo Feb 09 '21 at 08:37

2 Answers2

1
  1. It's not a NULL, it's a newline
  2. Yes! Just use the strip() method
Shay Nehmad
  • 1,103
  • 1
  • 12
  • 25
0

Just remove the final character:

your_list = ['name', 'AGATC', 'AATG', 'TATC\n']
print(your_list)  # ['name', 'AGATC', 'AATG', 'TATC\n']

your_list[-1] = re.sub(r'\n$', '', your_list[-1])
print(your_list)  # ['name', 'AGATC', 'AATG', 'TATC']
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360