简体   繁体   中英

How to remove \n and \t from a list in python?

I have been trying to remove \\n and \\t from 2 lists in python but have been unable to do so. Below is my code:

A=[]
B=[]
C=[]
D=[]
.................
.................

df=pd.DataFrame(A,columns=['Rank'])
df['Company Name']=B
C=list(filter(lambda x: x != '\n', C))
C=list(filter(lambda x: x != '\t', C))
df['Type of organization']=C
D=list(filter(lambda x: x != '\n', D))
D=list(filter(lambda x: x != '\t', D))
df['Industry']=D


writer = pd.ExcelWriter('compdata.xlsx', engine='xlsxwriter')
df.to_excel(writer, index=False, sheet_name='report')
writer.save()

Kindly help me out as I have tried lambda as well but to no avail. Every time I export the data frame to Excel, it gives me a lot of spaces in those 2 columns.

Below is how C and D looks like:

  Rank                 Company Name  \
0   1.            Google (Alphabet)   
1   2.                       ACUITY   
2   3.  The Boston Consulting Group   
3   4.         Wegmans Food Markets   

                                Type of organization  \
0  \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t...   
1  \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t...   
2  \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t...   
3  \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t...   

                                            Industry  
0  \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t...  
1  \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t...  
2  \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t...  
3  \n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t...  

In actuality, Type of organization should have either "Public" or "Private". It's just that the list has a lot of \\n and \\t before either of the aforementioned choices.

Try the following:

Using list comprehension :

my_list = ['a', 'b', '\n', 'c', '\t', 'd']

my_list = [item for item in my_list if item not in ['\n', '\t']]

Using filter() :

my_list = filter(lambda item: item not in ['\n', '\t'], my_list)

Edit:

For the example of inputs added to the question, you can remove \\t and \\n like this:

c = [''.join(item.split()) for item in c]

Output:

>>> c = ['\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t‌​\t\t\t\t\t\t\t\t\tPu‌​blic', '\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\‌​t\t\t\t\t\t\t\t\tPri‌​vate', '\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\‌​t\t\t\t\t\t\t\t\tPri‌​vate', '\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\‌​t\t\t\t\t\t\t\t\tPri‌​vate']
>>>
>>> [''.join(item.split()) for item in c]
['??Pu??blic', '\\??tPri??vate', '\\??tPri??vate', '\\??tPri??vate']

Try this:

C = [ x.replace('\t', '').replace('\n', '') for x in C ]

same for D

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