1 Replies - 464 Views - Last Post: 22 April 2013 - 10:05 PM Rate Topic: -----

#1 chan 06  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 07-October 12

How to make 2D array accessing to databse

Posted 22 April 2013 - 02:56 AM

Well I got a tough question, which I am unsure on how to apply my skills into 2D array.

So basically there is this list, with names and jobs they have:

This is a .txt document that I saved.
Jane | Teacher
Joe | Policeman
Alice | Geologist
Sher | Farmer

What I need to do is to read the document, and than print in 2D array.

So my question is how to append into 2D array?
[["Jane", "Teacher"], ["Joe", "Policeman"], ["Alice", "Geologist"], ["Alice", "Geologist"], ["Sher", "Farmer"]]

I know that when you read the text document is already stored in 1D array
["Jane Teacher", "Joe Policeman", "Alice Geologist", "Sher Farmer"]

so far I got this:
def clubList():
    list_jobs = open("text.txt", "r")
    lines = text_file.readlines()
    print (li)

    for line in li:
        print (line)

How it is stored in the array.
All it does, find the length of array.
Give me information about the array.

Is This A Good Question/Topic? 0
  • +

Replies To: How to make 2D array accessing to databse

#2 koborivers  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 19
  • Joined: 08-January 13

Re: How to make 2D array accessing to databse

Posted 22 April 2013 - 10:05 PM

Hi there chan,

It looks like you have a decent start,
    list_jobs = open("text.txt", "r")

is indeed how you open the file. From there you will need to loop over each line in the file.
I would recommend replacing
    lines = text_file.readlines()

with a for loop like so
for li in list_jobs:
    # li contains the current line
    # do things with the data here

this will read the file one line at a time instead of loading the whole thing.
As far as building your array (it's actually a list in python :)/>/> ), have a look at the split method for strings.
This will let you split the string based on a character ("|") into a list.

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1