Cybernetic
Cybernetic

Reputation: 13334

Strip outside quotes from text when writing to list in Python

I have a text file that looks like this when opened as file:

'element1', 'element2', 'element3', 'element4'

I then use a list comprehension to read this into a list

thelist = [line.strip() for line in open('file.txt', 'r')]

But the list looks like this with only one element at index 0

["'element1', 'element2', 'element3', 'element4'"]

Since the double quotes are appended to the ends of the first and last elements, python thinks it is a single element list. Is there a way to use the "strip()" inside the list comprehension to remove those outside double quotes?

Upvotes: 0

Views: 767

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121714

You don't have a list, you have a string that looks like a Python sequence.

You could use ast.literal_eval() to interpret that as a Python literal; it'll be parsed as a tuple of strings:

import ast

thelist = [ast.literal_eval(line) for line in open('file.txt', 'r')]

or you could just split on the comma, then strip the spaces and quotes:

thelist = [[elem.strip(" '") for elem in line.split(',')]
           for line in open('file.txt', 'r')]

This only works if your quoted values don't themselves contain commas.

Either way you get a list of lists; you could flatten that list:

thelist = [elem for line in open('file.txt', 'r')
                for elem in ast.literal_eval(line)]

or just read the one line:

thelist = ast.literal_eval(next(open('file.txt', 'r')))

You could also use the csv module:

import csv

reader = csv.reader(
    open('file.text', 'rb'),
    quotechar="'", skipinitialspace=True)
thelist = list(reader)

or for the first row only:

thelist = next(reader)

Upvotes: 1

Related Questions