-1

I have a file with one sigle long line. The lines contains nested list where each elements contains some integers and strings as given in the code example:

sl = [
    [127390,175493,530,1073310,2376580,"Mi:DR 96AII,Yt:DR 94A,AFA:DR 96b","26:17 tooth holes 
    (horizontal \/ vertical)\r\nwar print\r\nnew numbering, coarse impression, online YT catalog 
    2020","Unveiling of the monument of Emperor William I, Berlin"], 
    [127397,57201,530,2607693,2376580,"Mi:DR 103a,Sn:DE 101,Yt:DR 102,Sg:DR 103","","Germania,inscr. 
    DEUTSCHES REICH"]
]

print(sl,type(sl)) # type is list

with open('l1.txt', 'r') as f:
    l1_list = f.read() # the txt file contains the same as the variable sl 
    print(l1_list, type(l1_list)) # type is string

How can the file contents be interpreted as a list or How can the contents be converted from strings of characters to a list (comma separated)

Massimo Costa
  • 1,864
  • 15
  • 23
Kjell
  • 36
  • 3
  • 1
    Show us some sample content of the file! – Klaus D. Dec 28 '20 at 10:30
  • If that "one single long line" is a correct python list than you can do just eval. E.g. `eval("[['x','y','z'],[1,2,3]]")` – Alex Yu Dec 28 '20 at 10:45
  • Does this answer your question? [Split a string by a delimiter in python](https://stackoverflow.com/questions/3475251/split-a-string-by-a-delimiter-in-python) – Ignacio Alorre Dec 28 '20 at 10:45

2 Answers2

1

The string you present does appear to be a syntactically valid string representation of a Python list. If that is true for all of your data then this will do what you want:

import ast
...
with open('l1.txt', 'r') as f:
    l1text = f.read()
mylist = ast.literal_eval(l1text) 

I tested it like this:

>>> sl = r"""[[127390,175493,530,1073310,2376580,"Mi:DR 96AII,Yt:DR 94A,AFA:DR 96b","26:17 tooth holes (horizontal \/ vertical)\r\nwar print\r\nnew numbering, coarse impression, online YT catalog 2020","Unveiling of the monument of Emperor William I, Berlin"], [127397,57201,530,2607693,2376580,"Mi:DR 103a,Sn:DE 101,Yt:DR 102,Sg:DR 103","","Germania,inscr. DEUTSCHES REICH"]]"""
>>> x = ast.literal_eval(sl)
>>> x
[[127390, 175493, 530, 1073310, 2376580, 'Mi:DR 96AII,Yt:DR 94A,AFA:DR 96b', '26:17 tooth holes (horizontal \\/ vertical)\r\nwar print\r\nnew numbering, coarse impression, online YT catalog 2020', 'Unveiling of the monument of Emperor William I, Berlin'], [127397, 57201, 530, 2607693, 2376580, 'Mi:DR 103a,Sn:DE 101,Yt:DR 102,Sg:DR 103', '', 'Germania,inscr. DEUTSCHES REICH']]
BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Just a question: builtin `eval` will work the same way - right? – Alex Yu Dec 28 '20 at 10:48
  • 1
    Yes, it will. But unless you can be *very* sure that the expression you want evaluated is not malicious, `ast.literal_eval()` is safer, because it will only evaluate literals, and will not call functions that you might not want to be called. So as a matter of good practice you only use `eval()` if it is strictly necessary and if you can definitely trust the string you are passing it. In this particular case the caution is completely unnecessary, but it's prudent to recommend to beginners that they reach first for `ast.literal_eval()`. – BoarGules Dec 28 '20 at 11:05
0

I think you want to turn out that 2D list to 1D list. Here my suggestion:

s1 = sum(s1, [])
Halley
  • 11
  • 3