-2

I have a python list object containing elements as follows:

[u'Sentence 1 blabla.'],
[u'Sentence 2 blabla.'],
...

I want to extract the contents inside ' ' present in the list and store each as separate lines in a text file. Please help me with the same

Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36
Anu
  • 119
  • 2
  • 11

2 Answers2

1

u'blablabla' is Unicode. You can convert it into string using str(unicode)

Example:

a = [[u'qweqwe'],[u'asdasd']]

str(a[0][0]) will be string qweqwe.
Now you can write it into file as usual.

Try this example for clarity:

a = [[u'qweqwe'],[u'asdasd']]
print type(a[0][0])
print type(str(a[0][0]))

Output:

<type 'unicode'>
<type 'str'>
Jithin Pavithran
  • 1,250
  • 2
  • 16
  • 41
1

Firstly, Given that you have a list of lists, I am going to assume that you are going to convert the given list from unicode to an UTF8 string. For this lets have a function convertList which takes a list of lists as an input

So your initial value for l would be

[[u'Sentence 1 blabla.'],
[u'Sentence 2 blabla.'],
...]

Notice that this is a list of lists. Now for each list item, loop through the items within that list and convert them all to UTF8.

def convertList(l):
    newlist = []
    for x in l:
        templ = [item.encode('UTF8') for item in x]
        newlist.append(templ)
    return newlist

l = [[u'Sentence 1 blabla.'], [u'Sentence 2 blabla.']]
l = convertList(l) # This updates the object you have
# Now do the filewrite operations here.
f = open('myfile.txt','w')
for iList in l:
    for items in iList:
        f.write(items+'\n')
f.close()

This will write to the file myfile.txt as follows

Sentence 1 blabla. Sentence 2 blabla.

Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36