I have a data like this . the strings are separated by comma.
"India1,India2,myIndia "
"Where,Here,Here "
"Here,Where,India,uyete"
"AFD,TTT"
What I am trying to do is to put them all in one column (one under each other) So it will become like this
India1
India2
myIndia
Where
Here
Here
Here
Where
India
uyete
AFD
TTT
Then I keep the unique ones which lead to this
India1
India2
myIndia
Where
Here
India
uyete
AFD
TTT
So I have the first data in a .txtformat and I have tried to use numpyfor this
This is my code
#!/usr/bin/python
import numpy as np# give a name to my data
file_name = 'path to my data/test.txt'
# set my output
with open ( 'output.txt' , 'w' ) as out:# read all the linesfor n , line in enumerate ( open ( file_name ).readlines ( ) ):# split each stirg from another one by a commaitem1 = file_name.split ( ',' )myList = ','.join ( map ( str , item1 ) )item2 = np.unique ( myList , return_inverse=True )# save the data into outout.write ( item2 )
I was getting TypeError: expected a character buffer object
I have searched it and I found several post like TypeError: expected a character buffer object - while trying to save integer to textfile
and If I added out.seek ( 0 )
I still got the same error
but by changing it to out.write ( str(item2 ))
thanks to TypeError: expected a character buffer object I get no error however, the output is showing this
(array(['/path to the file/test.txt'], dtype='|S29'), array([0]))
Below is given a soltuion which I tried to use
import csvdata = []
def remove_quotes(file):for line in file:yield line.strip ( '"\n' )
with open ( 'test.txt' ) as f:reader = csv.reader ( remove_quotes ( f ) )for row in reader:data.extend ( row )
No error but also data
is not generated