-2

I cannot figure out to solve this problem even though it seems to be an easy one.

I have a scraped database in the txt file where entries for author-publications are stored like this:

    ['Radoslav Bajus'],[['Economic Annals-XXI', '2016-06-21'], ['Actual Problems of Economics', '2016-01-01'], ['Actual Problems of Economics', '2015-01-01'], ['Actual Problems of Economics', '2015-01-01'], ['Accounting Reform in Transition and Developing Economies', '2009-12-01']]

I originally intended to loop through the lists. But ofc when I read txt I end up with:

    "['Alena Andrejovská'],[['Economic Annals-XXI', '2016-10-10'], ['Journal of Applied Economic Sciences', '2016-09-01'], ['Acta Universitatis Agriculturae et Silviculturae Mendelianae Brunensis', '2016-01-01'], ['Agris On-line Papers in Economics and Informatics', '2016-01-01'], ['Actual Problems of Economics', '2016-01-01'], ['Acta Universitatis Agriculturae et Silviculturae Mendelianae Brunensis', '2015-01-01'], ['Actual Problems of Economics', '2015-01-01'], ['Journal of Applied Economic Sciences', '2015-01-01'], ['Journal of Applied Economic Sciences', '2014-01-01'], ['Journal of Applied Economic Sciences', '2013-01-01']]"

Is there the way to get read of quotation marks and the string format? The code I use is the following:

    fh = open('results_publications.txt', encoding = "utf8")
    lst = []
    for line in fh:
        lst.append(line.strip())

UPD Ok, sorry for not being clear. I have a .txt file where I have the following entries separated by /n (see three entries below).

['Alena Andrejovská'],[['Economic Annals-XXI', '2016-10-10'], ['Journal of Applied Economic Sciences', '2016-09-01'], ['Acta Universitatis Agriculturae et Silviculturae Mendelianae Brunensis', '2016-01-01'], ['Agris On-line Papers in Economics and Informatics', '2016-01-01'], ['Actual Problems of Economics', '2016-01-01'], ['Acta Universitatis Agriculturae et Silviculturae Mendelianae Brunensis', '2015-01-01'], ['Actual Problems of Economics', '2015-01-01'], ['Journal of Applied Economic Sciences', '2015-01-01'], ['Journal of Applied Economic Sciences', '2014-01-01'], ['Journal of Applied Economic Sciences', '2013-01-01']]
['Radoslav Bajus'],[['Economic Annals-XXI', '2016-06-21'], ['Actual Problems of Economics', '2016-01-01'], ['Actual Problems of Economics', '2015-01-01'], ['Actual Problems of Economics', '2015-01-01'], ['Accounting Reform in Transition and Developing Economies', '2009-12-01']]
['Ľudmila Bartóková'],[['Journal of Applied Economic Sciences', '2016-03-01'], ['E a M: Ekonomie a Management', '2015-01-01'], ['Journal of Applied Economic Sciences', '2013-01-01'], ['12th International Multidisciplinary Scientific GeoConference and EXPO - Modern Management of Mine Producing, Geology and Environmental Protection, SGEM 2012', '2012-12-01'], ['Journal of Applied Economic Sciences', '2012-12-01'], ['E a M: Ekonomie a Management', '2010-06-21']]

I want to read this txt file into the list to be able to loop through it later. But when you read txt file it converts each entry into the string and puts " ". I am trying to figure out how to convert it to the list of lists, not a list of strings.

P Raju
  • 329
  • 2
  • 14
Tatiana
  • 1
  • 1

1 Answers1

1

You can convert from a string back to Python data like

import ast

data_string = "['Alena Andrejovská'],[['Economic Annals-XXI', '2016-10-10'], ['Journal of Applied Economic Sciences', '2016-09-01'], ['Acta Universitatis Agriculturae et Silviculturae Mendelianae Brunensis', '2016-01-01'], ['Agris On-line Papers in Economics and Informatics', '2016-01-01'], ['Actual Problems of Economics', '2016-01-01'], ['Acta Universitatis Agriculturae et Silviculturae Mendelianae Brunensis', '2015-01-01'], ['Actual Problems of Economics', '2015-01-01'], ['Journal of Applied Economic Sciences', '2015-01-01'], ['Journal of Applied Economic Sciences', '2014-01-01'], ['Journal of Applied Economic Sciences', '2013-01-01']]"

data = ast.literal_eval(data_string)
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99