0

I need help to fetch list back from a file where each line is stored as a list. For example, check the following lists.txt file

["1","Andy Allen","Administrator"]
["2","Bob Benny","Moderator"]
["3","Zed Zen","Member"]

I need the content to be accessible and be displayed in the following manner

SN : 1
Name : Andy Allen
Position : Administrator

SN : 2
Name : Bob Benny
Position : Moderator

SN : 3
Name : Zed Zen
Position : Member

PS : I know I could have saved it with Delimiters and access list elements with split function... But it seems that when using a particular delimiter as the text content causes the function to function abnormally..

c0degeas
  • 762
  • 9
  • 19
  • what have you done so far? What is the problem that you are facing? – Aseem Bansal Jul 20 '16 at 18:08
  • do you have existing code that you've tried? – Abhi V Jul 20 '16 at 18:08
  • "*PS : I know I could have saved it with Delimiters and access list elements with split function... But it seems that when using a particular delimiter as the text content causes the function to function abnormally..*" - that's why you use an existing module to export and import data, because other people have already done the work of properly dealing with quoting and escaping of delimiter characters in field data. Python even comes with a `csv` module. – TessellatingHeckler Jul 20 '16 at 18:10
  • Basically what I wanna know here is to convert a string (which is stored in a list like structure) into an actual list.. – c0degeas Jul 20 '16 at 18:11
  • Possible duplicate of [python string list to list ast.listeral\_eval](http://stackoverflow.com/questions/10113270/python-string-list-to-list-ast-listeral-eval) – TessellatingHeckler Jul 20 '16 at 18:12
  • `when using a particular delimiter as the text content causes the function to function abnormally` was the delimiter spaces or comma? Read TessellatingHeckler's comment again. What he is saying would help you. – Aseem Bansal Jul 20 '16 at 18:13
  • @Tessie CSV doesn't surround each row with square brackets. It looks like JSON, and I've answered as such. – Damian Yerrick Jul 20 '16 at 18:15
  • @TessellatingHeckler It's not exactly duplicate of that question but Found my solution there.. Using the function literal_eval(line) under ast library.. Thanks !! – c0degeas Jul 20 '16 at 19:08
  • @DamianYerrick the data isn't CSV, but the OP says they had control of choosing a format to save in, and instead of using a standard format with a library to support it, they made up an export-format which they don't know how to read back in, out of a fear of CSV which is unfounded (and their format doesn't include the column headers they need). I was strongly recommending saving as CSV, and using the existing module because addresses the bit of CSV they were afraid of. – TessellatingHeckler Jul 20 '16 at 19:14
  • @TessellatingHeckler you mean the delimiter problem can be solved best with csv datas ? – c0degeas Jul 20 '16 at 19:31
  • @PrashantShahi Yes, it's possible to solve the delimiter problem in CSV, and I've edited how to do so into my answer. – Damian Yerrick Jul 20 '16 at 20:25
  • @DamianYerrick Thanks, that'd do the job !! – c0degeas Jul 20 '16 at 20:29

2 Answers2

1

This format, using only double quotes and not single, looks like JSON (JavaScript object notation). Fortunately, the json module that comes with Python 2.6 or later can parse JSON. So read each line, parse it as JSON (using json.loads), and then print it out. Try something like this:

import json
with open("lists.txt", "r") as infp:
    rows = [json.loads(line) for line in infp]
for sn, name, position in rows:
    print("SN : %s\nName : %s\nPosition : %s\n" % (sn, name, position))

Taming CSV

Another solution is to export in tab- or comma-separated values formats and use Python's csv module to read them. If (for example) a value in a comma-separated file contains a comma, surround it with double quotes ("):

2,Bob Benny,"Moderator, Graphic Designer"

And if a value contains double quote characters, double them and surround the whole thing with double quotes. For example, the last element of the following row has the value "Fossils" Editor:

5,Chester from Tilwick,"""Fossils"" Editor"

A spreadsheet app, such as Excel, LibreOffice Calc, or Gnumeric, will do this escaping for you when saving in separated format. Thus lists.csv would look like this:

1,Andy Allen,Administrator
2,Bob Benny,"Moderator, Graphic Designer"
3,Zed Zen,Member
5,Chester from Tilwick,"""Fossils"" Editor"

Which can be parsed similarly:

import csv
with open("lists.csv", "r", newline="") as infp:
    # use "excel" for comma-separated or "excel-tab" for tab-separated
    reader = csv.reader(infp, "excel")
    rows = list(reader)
for sn, name, position in rows:
    print("SN : %s\nName : %s\nPosition : %s\n" % (sn, name, position))

(This is for Python 3. Python 2's csv module differs slightly in that open should be binary: open("lists.txt", "rb").)

Damian Yerrick
  • 4,602
  • 2
  • 26
  • 64
0

How about using grep to grab all the contents inside the [ ]?

danialcala
  • 11
  • 2