x404.co.uk
http://www.x404.co.uk/forum/

Python
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=10185
Page 1 of 1

Author:  Angelic [ Wed Aug 25, 2010 12:03 pm ]
Post subject:  Python

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

Author:  EddArmitage [ Wed Aug 25, 2010 12:37 pm ]
Post subject:  Re: Python

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')]

Author:  Angelic [ Wed Aug 25, 2010 1:57 pm ]
Post subject:  Re: Python

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?

Author:  EddArmitage [ Wed Aug 25, 2010 2:14 pm ]
Post subject:  Re: Python

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)

Author:  Angelic [ Wed Aug 25, 2010 2:41 pm ]
Post subject:  Re: Python

Well that hasn't worked on its own, but I shall certainly read around it and report back with my findings!

Author:  EddArmitage [ Wed Aug 25, 2010 2:45 pm ]
Post subject:  Re: Python

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!

Author:  Angelic [ Thu Aug 26, 2010 12:12 pm ]
Post subject:  Re: Python

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...

Author:  EddArmitage [ Thu Aug 26, 2010 12:17 pm ]
Post subject:  Re: Python

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!

Author:  Angelic [ Thu Aug 26, 2010 3:45 pm ]
Post subject:  Re: Python

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 >.<

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/