Reply to topic  [ 9 posts ] 
Python 
Author Message
Doesn't have much of a life
User avatar

Joined: Thu Apr 23, 2009 7:16 pm
Posts: 704
Location: Leeds, UK
Reply with quote
Hey guys,

Muddling through some Python, wondered if anyone here could help a little bit?

Basically what i'm trying to do is open one .txt file and input the values into a list in Python.

Then i'm opening another .txt file, and inputting all those values into another list in Python (but one that preserves the table structure).

What I want to do is take a number from the first text file and use it to print that number line from the second.

For instance:

Text file contains just the number "4".
Text file 2 contains rows which read:

0, MON, 0800,
1, MON, 0830,
2, MON, 0900,
3, MON, 0930,
4, MON, 1000,
[etc]

And all I want to happen is that 5th row to be printed...

Anyone know how yet?

Here's what i've got so far:

Code:
f = open ('test.txt', 'r')
period = f.readlines ()
f.close ()

# list containing user timetable info is called "period"

g = open ('sample.txt', 'r')
schedper = g.read ()
g.close ()

# list containing scheduled period info is called "schedper"

if period == line in schedper[0]:
    print schedper.pop()


but, obviously, it doesn't work!

I just want SOMETHING to be returned >.<

But i've been up all night trying to do this and don't seem to have progressed.. I mean I started at 2pm yesterday and would like something working before the 24 hour mark! Lol


Wed Aug 25, 2010 12:03 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
The uni VPN is down today, so I can't get at my old python stuff, but looking at this, and going a bit from memory, I guess you could try something like:

Code:
schedper = [line.split(',') for line in open('./MY_CSV_FILE.csv')]

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Wed Aug 25, 2010 12:37 pm
Profile
Doesn't have much of a life
User avatar

Joined: Thu Apr 23, 2009 7:16 pm
Posts: 704
Location: Leeds, UK
Reply with quote
You've definitely pointed me in the right direction, I now have this:

Code:
import csv

schedper = csv.reader(open("sample.csv", "rU"), dialect=csv.excel_tab)

for row in schedper:
    print row


This brings me a list of all the rows, in the following format:

Code:
['0,0800,MON']
['1,0830,MON']
['2,0900,MON']
['3,0930,MON']
['4,1000,MON']


How do you reckon I would get it to print a certain line?


Wed Aug 25, 2010 1:57 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
Could you not assign a value to a variable index? I presume you then want to loop through the first file (containing indices) and print those rows of the second file? Could you not do something like:

Code:
for index in period
    print  schedper.pop(index)

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Wed Aug 25, 2010 2:14 pm
Profile
Doesn't have much of a life
User avatar

Joined: Thu Apr 23, 2009 7:16 pm
Posts: 704
Location: Leeds, UK
Reply with quote
Well that hasn't worked on its own, but I shall certainly read around it and report back with my findings!


Wed Aug 25, 2010 2:41 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
If I get up to campus tonight I'll have a look for you. At least then I'll actually have a python interpretter, and not be coding in C++ at the same time!

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Wed Aug 25, 2010 2:45 pm
Profile
Doesn't have much of a life
User avatar

Joined: Thu Apr 23, 2009 7:16 pm
Posts: 704
Location: Leeds, UK
Reply with quote
Well i've made huge progress! Not exactly the most gripping of subjects, but if anyone wants to see how I did it I can post what i've done...


Thu Aug 26, 2010 12:12 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
Angelic wrote:
Well i've made huge progress! Not exactly the most gripping of subjects, but if anyone wants to see how I did it I can post what i've done...

Sure. It'd be good to revive my scripting skills a bit!

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Thu Aug 26, 2010 12:17 pm
Profile
Doesn't have much of a life
User avatar

Joined: Thu Apr 23, 2009 7:16 pm
Posts: 704
Location: Leeds, UK
Reply with quote
Well this is what I have so far...
Code:
#Import the data from the .csv

import csv
query = csv.reader (open('query.txt', 'rb'), delimiter=',', quotechar='|')
query_list = []
query_list.extend(query)

#Import the data from the scheduled_periods document

reference = open('reference.txt', 'r')

#Create some lists to work with

module_titles = []
duration = []
scheduled_periods = []

#Take the relevant data from the .csv and put them into the correct lists

for data in query_list:
    module_titles.append(data[0][0:11])

for data in query_list:
    scheduled_periods.append(data[-2][-5:-3])

for data in query_list:
    duration.append(data[-1:])
#
#To check the lists are correct, the following can be used:
#
#for line in scheduled_periods:
#    if line.startswith("("):
#        print line[1]
#    else:
#        print line
#   
#for line in duration:
#    print line
#
#for row in module_titles:
#    print row
#
#for line in scheduled_periods:
#    if line.startswith("("):
#        print line[1]
#    else:
#        print line


The next bit is what i'm going to have trouble with:

Code:
for line in reference:
    if line.startswith(str(scheduled_periods)):
        print line       
    else:
            print "fail"
            break


How would I go about this? What is basically happening is i'm looking down all the lines in the list "reference" (which was imported right at the beginning) and for each line in "scheduled_periods" I need to output the matching data from.

Put simply (and not ridiculously, like my last paragraph):

Reference contains the data what we've seen before:
Code:
0,0800,MON
1,0830,MON
2,0900,MON
3,0930,MON
etc..


Scheduled_Periods contains the data:
Code:
3
1
1
2
etc..


And I would very much like to be printed to screen:
Code:
3,0900,MON
1,0830,MON
1,0830,MON
2,0900,MON


Obviously it'll be running on a much larger scale (numbers going up to 149) and have about 10-30 results to sift through but i'm not looking for anything that's particularly CPU-friendly, just need it to work >.<


Thu Aug 26, 2010 3:45 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.