473,382 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Beginner question: use function to read text file

I'm pretty stuck at the moment and wondering if anyone can spot the problem.
Trying to create a function that will read a text file into a list and
return that list.

I wrote the following function and saved it as 'fileloader.py'

def fileload(fname):
infile=open(fname)
dates =[]
times=[]
open=[]
high=[]
low=[]
close=[]
vol=[]
count=0
for line in infile:
item=line.split()
dates.append(item[0])
times.append(item[1])
open.append(item[2])
high.append(item[3])
low.append(item[4])
close.append(item[5])
vol.append(item[6])
#print
dates[count],times[count],open[count],high[count],low[count],vol[count]
count=count+1
return dates,times,open,high,low,close
Then I executed the following script (merge contract v1.py):

import fileloader
filename='c:/Python24/test/testdata2.txt'
fileloader.fileload(filename)
I then get the following error messages:

Traceback (most recent call last)
File "C:\Python24\test\merge contract v1.py", in line3, in?
fileloader.fileload(filename)
File ("C:\Python24\text\fileloader.py", in line2, in fileload
infile=open(fname)
UnboundLocalError: local variable 'open' referenced before assignment
Script terminated

Thanks for any help,
Luke
Jun 27 '06 #1
3 1814
Luke wrote:
I'm pretty stuck at the moment and wondering if anyone can spot the problem.
Trying to create a function that will read a text file into a list and
return that list.

I wrote the following function and saved it as 'fileloader.py'

def fileload(fname):
infile=open(fname)
dates =[]
times=[]
open=[]
high=[]
low=[]
close=[]
vol=[]
count=0
for line in infile:
item=line.split()
dates.append(item[0])
times.append(item[1])
open.append(item[2])
high.append(item[3])
low.append(item[4])
close.append(item[5])
vol.append(item[6])
#print
dates[count],times[count],open[count],high[count],low[count],vol[count]
count=count+1
return dates,times,open,high,low,close
Then I executed the following script (merge contract v1.py):

import fileloader
filename='c:/Python24/test/testdata2.txt'
fileloader.fileload(filename)
I then get the following error messages:

Traceback (most recent call last)
File "C:\Python24\test\merge contract v1.py", in line3, in?
fileloader.fileload(filename)
File ("C:\Python24\text\fileloader.py", in line2, in fileload
infile=open(fname)
UnboundLocalError: local variable 'open' referenced before assignment
Script terminated

Thanks for any help,
Luke


def fileload(fname):
infile=open(fname) # <===
dates =[]
times=[]
open=[] # <===

You have assigned open (which, by the way, is a builtin!) in a function
*after* you have referenced it. You have over-ridden the open name with
the assignment, but you have referenced it 'before assignment', as your
error mesage says. This is a favorite trip-up of newer pythong
programmers. Perhaps replace

open=[]

with something like

open_=[]

etc.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Jun 27 '06 #2
Luke wrote:
I'm pretty stuck at the moment and wondering if anyone can spot the problem.
Trying to create a function that will read a text file into a list and
return that list.

I wrote the following function and saved it as 'fileloader.py'

def fileload(fname):
infile=open(fname)
dates =[]
times=[]
open=[]
Here's the problem. You are using the name "open" it two contexts. The
variable named "open" is a local variable, and as such it hides the
builtin function of the same name used to open files. (It does not
matter that the use of "open" as a function precedes the use of "open"
as a variable name. Python is rather strict about this. If you use a
variable in a def, then ANYWHERE in that def, that name refers to the
local variable and hides any uses of the name from enclosing scopes.)
If that last makes sense, good. If not, then just follow this rule:
Choose your variable names to be different than any builtin names -- or
at least different than any of the builtin names you intend to use.

Gary Herron
high=[]
low=[]
close=[]
vol=[]
count=0
for line in infile:
item=line.split()
dates.append(item[0])
times.append(item[1])
open.append(item[2])
high.append(item[3])
low.append(item[4])
close.append(item[5])
vol.append(item[6])
#print
dates[count],times[count],open[count],high[count],low[count],vol[count]
count=count+1
return dates,times,open,high,low,close
Then I executed the following script (merge contract v1.py):

import fileloader
filename='c:/Python24/test/testdata2.txt'
fileloader.fileload(filename)
I then get the following error messages:

Traceback (most recent call last)
File "C:\Python24\test\merge contract v1.py", in line3, in?
fileloader.fileload(filename)
File ("C:\Python24\text\fileloader.py", in line2, in fileload
infile=open(fname)
UnboundLocalError: local variable 'open' referenced before assignment
Script terminated

Thanks for any help,
Luke


Jun 27 '06 #3
Thanks to both of you for the help, much appreciated!

Luke
Jun 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 # number of balls 33 42 13
8
by: jimscott77 | last post by:
Can someone help with the following problem? Original Question: TextBox1 is bound to a dataset and at runtime shows a value of "5" I am trying to use that value as follows: TextBoxA.Text =...
9
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am...
8
by: AG | last post by:
Hello, This is my first post to this group, and on top of that I am a beginner. So please direct me to another group if this post seems out of place.... I have recently written a program which...
9
by: raam | last post by:
hi all, I trying to write an array of integers into a text file.While writing into the file it works.I used fwrite function, but when I read the same file using fread or fscanf it fails and it...
3
by: Jeff | last post by:
....another beginnger question. I have a web application in .net v2 VB that requires multiple reads from sql tables where each read is slightly different - so the sql select statements also differ...
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
4
by: isaac2004 | last post by:
hello, what i am trying to do is set up a loop that grabs multiple variables from an input file. my question is, is there a built in function in the istream class that can grab multiple values on...
1
by: =?utf-8?q?C=C3=A9dric_Lucantis?= | last post by:
Le Wednesday 02 July 2008 01:16:30 Ben Keshet, vous avez écrit : If the file you're reading is not too big, you can use file.readlines() which read all the files and returns its content as a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.