简体   繁体   English

如何摆脱\n?

[英]how to get rid of \n?

'Account\n688997' is one string of many strings in my list and this happens more than once. 'Account\n688997'是我列表中许多字符串中的一个,而且这种情况不止一次发生。 I've tried replace(r'\n',',') , I've tried doing a for loop with .strip() and I can get it to print without it (naturally) but I can't get it to print out in the list without the \n .我试过replace(r'\n',',') ,我试过用.strip()做一个 for 循环,我可以让它在没有它的情况下打印(自然)但我不能让它在没有\n的列表中打印出来。 What I would like is ['Account','688997'] but what I always get is ['Account\n688997'] .我想要的是['Account','688997']但我总是得到的是['Account\n688997']

You can use string split function to split lists based on the newline:您可以使用字符串拆分 function根据换行符拆分列表:

>>> s = 'Account\n688997'
>>> s.split()
['Account', '688997']
>>> # You can also make the split more explicit by passing the `\n` to the `str.split` function
>>> s.split('\n')
['Account', '688997']

The split() is suitable for your problem, split()适合您的问题,

print('Account\n688997'.split())

output: output:

['Account', '688997']

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

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