0

i want to implement the following in python, in C, it is like below, there is a structure

struct xyz {
char name[50];
char type[50];
char link[50];
char level[50];
}XYZ;

And i have created an array of this struct xyz as following:

XYZ array[] = 
{ {"Mac", "char", "list","one"},
  {"John", "char", "list","three"},
  ...
  ...
};

and accessing these by array[0], array[1] etc. In python script, suppose i have listed these array elements in a text file like below, e.g. file.txt

Mac, char, list, one
John, char, list, three
...
...

Now i have to read the file.txt, and store them into my python script similar to array of structs and access accordingly.

4 Answers 4

1
import csv

with open('somefile.txt') as f:
   reader = csv.reader(f, delimiter=',')
   lines = list(reader)

print(lines)
Sign up to request clarification or add additional context in comments.

Comments

1

On top of what was suggested here, you might want to take advantage of the fact that Python can do OOP (as opposed to C), so adding to Burjan's answer, I would do something like:

class xyz():
def __init__(self, name, type):
    self.name = name
    self.type = type
    // etc

And then call something like result = [ xyz(*line) for line in lines ]

Comments

0

Your syntax is a bit off.

This will create an array of arrays of 4 values:

XYZ = [ 
    ["Mac", "char", "list","one"],
    ["John", "char", "list","three"],
    ...
    ...
]

This will create an array of objects with 4 fields:

XYZ = [ 
    {"name": "Mac", "type": "char", "link": "list", "level": "one"},
    {"name": "John", "type": "char", "link": "list", "level": "three"},
    ...
    ...
]

To read this data from file into structure like #2:

import csv

XYZ = []
with open("data.csv") as csv_data:
    entries = csv.reader(csv_data, delimiter=",")
    # This can be done with list comprehension, but will be difficult to read
    for entry in entries:
        XYZ.append({
            "name": entry[0],
            "type": entry[1],
            "link": entry[2],
            "level": entry[3]
        })

6 Comments

His syntax is C, not Python, which is why it looks so odd as Python. :D
thanks a lot,it is similar to what i was trying to do but how would i get the corresponding value? meaning if i search the string 'Mac' in XYZ, and if found then i need the corresponding type, link and level at the same time..
if you need to search for "name=Mac": [entry for entry in XYZ if entry.name == "Mac"] will return you the list of corresponding entries.
Please don't mind, i am a beginner i python[entry for entry in XYZ if entry.name == "Mac"] AttributeError: 'dict' object has no attribute 'name'
working but i am not getting the right values, it prints the values of last index e.g. char, John, list, three
|
0
import csv

from collections import namedtuple
XYZ = namedtuple('xyz', ['name', 'type', 'link', 'level'])

with open('somefile.txt') as f:
   reader = csv.reader(f, delimiter=',')
   lines = [XYZ(*line) for line in reader]

for item in lines:
    print(item.name)
    print(item.type)
    #etc.

2 Comments

thanks, i tried it but getting error: lines = [XYZ(line) for line in reader] TypeError: __new__() takes exactly 5 arguments (2 given)
@Jaiswal, that should be lines = [XYZ(*line) for line in reader]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.