Splitting a line from a file into different lists - python

My program is supposed to take input from the user and read a file with the name input. Read file gets saved into a dictionary called portfolio and from there all I have to do is sort each line in the portfolio into keys and values.
Here's my code.
portfolio = {}
portfolio = file_read() #Reads the file through a function
if file_empty(portfolio) == True or None: #nevermind this, it works
print "The file was not found."
else:
print "The file has successfully been loaded"
for line in portfolio:
elements = line.strip().split(",") #separate lists by comma
print elements[0] #using this to check
print elements[1] #if it works at all
All this does is print the first letter in the first line, which is S. And apparently elements[1] is supposed to be the second letter but index is out of range, please enlighten me what might be wrong.
Thank you.

It looks like file_read() is reading the file into a string.
Then for line in portfolio: is iterating through each character in that string.
Then elements = line.strip().split(",") will give you a list containing one character, so trying to get elements[1] is past the bounds of the list.
If you want to read the whole contents of the file into a string called portfolio, you can iterate through each line in the string using
for line in porfolio.split('\n'):
...
But the more usual way of iterating through lines in a file would be
with open(filename,'r') as inputfile:
for line in inputfile:
....

Got it to work with this code:
for line in minfil :
line = line.strip()
elements = line.split(",")
portfolio[str(elements[0])] = [(int(elements[1]),float(elements[2]), str(elements[3]))]

Related

Error in Python Code Trying To Open and Access a File

Here is info from the .txt file I am trying to access:
Movies: Drama
Possession, 2002
The Big Chill, 1983
Crimson Tide, 1995
Here is my code:
fp = open("Movies.txt", "r")
lines = fp.readlines()
for line in lines:
values = line.split(", ")
year = int(values[1])
if year < 1990:
print(values[0])
I get an error message "IndexError: list index out of range". Please explain why or how I can fix this. Thank you!
Assuming your .txt file includes the "Movies: Drama" line, as you listed, it's because the first line of the text file has no comma in it. Therefore splitting that first line on a comma only results in 1 element (element 0), NOT 2, and therefore there is no values[1] for the first line.
It's not unusual for data files to have a header line that doesn't contain actual data. Import modules like Pandas will typically handle this automatically, but open() and readlines() don't differentiate.
The easiest thing to do is just slice your list variable (lines) so you don't include the first line in your loop:
fp = open("Movies.txt", "r")
lines = fp.readlines()
for line in lines[1:]:
values = line.split(", ")
year = int(values[1])
if year < 1990:
print(values[0])
Note the "lines[1:]" modification. This way you only loop starting from the second line (the first line is lines[0]) and go to the end.
The first line of the text file does not have a ", ", so when you split on it, you get a list of size 1. When you access the 2nd element with values[1] then you are accessing outside the length of the array, hence the IndexError. You need to do a check on the line before making the assumption about the size of the list. Some options:
Check the length of values and continue if it's too short.
Check that ', ' is in the line before splitting on it.
Use a regex which will ensure the ', ' is there as well as can ensure that the contents after the comma represent a number.
Preemptively strip off the first line in lines if you know that it's the header.
Your first line of your txt file has wrong index
Just simple change your code to:
fp = open("Movies.txt", "r")
lines = fp.readlines()
for line in lines:
try: #<---- Here
values = line.split(", ")
year = int(values[1])
if year < 1990:
print(values[0])
except: #<--------And here
pass

how can i read each word of a line from a file separately?

so the problem I came across is that I need to read each word of a line from a line one by one and repeat it for the whole file. each of the words are separated from each other by the # sign, e.g
2016/2017#Southeast_Kootenay#Mount_Baker_Secondary#STANDARD#COURSE_MARKS#99.0#71.0#88.0#49.0
after that I need to assign each value to the appropriate element of a class, for example:
school_years would be 2016/2017, district_name would be Southeast_Kootenay and etc.
the thing is that I have clue how to do it, I managed to extract the first word from a file but couldn't do it for the whole line and let alone the whole file, this is the code I used.
def word_return():
for lines in file:
for word in lines.split('#'):
return word
any kind of help would be appreciated
You're returning a single word. Remove last for and return the entire list like this if you want to get only the first line:
(Assuming file is a list of lines resulted from file = open("file.txt", "r").readlines())
def word_return():
for line in open("yourFile.txt", "r").readlines():
return lines.split('#')
If you want to return a list that will contain a list for each line, check the following:
def word_return():
allLines = []
for line in open("yourFile.txt", "r").readlines():
allLines.append(lines.split('#'))
return allLines

Python: How to return specific lines from a text

I am new here and new to Programming too.
I am reading Jamie Chan's Learn Python in One Day and am currently at the Practical Project section. I am trying to make python read a line from a txt file. The txt file contains a name and a number seperated by a comma,
This is the text file
Benny, 102
Ann, 100
Carol, 214
Darren, 129
I succeded in making it read the first line but the trying to print the second line by calling on the name there keeps returning a nill. When I switch the lines, the same thing occurs, it reads the name in the first line but returns nill on the name in the second file.
This is the function I tried to use to read the texts:
def getUserPoint(userName):
f = open('userScores.txt', 'r')
for line in f:
result = line.splitlines()
if userName in line:
return result
else:
return "nill"
f.close()
s = getUserPoint(input('Ann'))
print(s)
And this is the result:
nill
and this is the instructions:
Each line records the information of one user. The first value is the user’s username and the second is the user’s score.
Next, the function reads the file line by line using a for loop. Each line is then split using the split() function
Let’s store the results of the split() function in the list content.
Next, the function checks if any of the lines has the same username as the value that is passed in as the parameter. If there is, the function closes the file and returns the score beside that username. If there isn’t, the function closes the file and returns the string ‘-1’
Am terribly sorry for the long winded post.
you can use :
def getUserPoint(userName):
f = open('userScores.txt', 'r')
for line in f.readlines():
result = line.splitlines()
if userName in line:
f.close()
return result
f.close()
return "nill"
s = getUserPoint(input('Ann'))
print(s)
One problem is that you have an else statement that is matched and will immediately end the function and loop
You need to return the default result after you've looked at all lines
def getUserPoint(userName):
with open('userScores.txt') as f:
for line in f:
if userName == line.rstrip().split(',')[0]:
return line
return "nill"
Then, as shown, you either want to split the comma and check the first column, or userName in line . Otherwise, you are checking
'Ann' in ["Ann, 100", ""]
since splitlines() will split at the newline character at the end, which returns False
See below
The code takes care of closing the file.
It will return None if no match found, else 'user point' is returned
def get_user_point(user_name):
with open('userScores.txt', 'r') as f:
lines = [l.strip() for l in f]
for line in lines:
parts = line.split(',')
if user_name == parts[0]:
return parts[1]
Thanks everyone for the help...
This code by OneCricketeer worked:
def getUserPoint(userName):
with open('userScores.txt') as f:
for line in f:
if userName == line.split(',')[0]:
return line
return "nill"
Since am new to Python and programming in General, I will probably be asking a lot more questions.
Thanks for the help everyone.

Reading a text file then storing first and third word as key and value respectively in a dictionary

Here's the code:
def predator_eat(file):
predator_prey = {}
file.read()
for line in file:
list = file.readline(line)
list = list.split(" ")
predator_prey[list[0]] = list[2]
print(predator_prey)
predator_eat(file)
The text file is three words per line with \n at the end of each line and I previously opened the file and stored the file name as the variable file
using file = open(filename.txt, r)
the print statement ends up being an empty dictionary so it isn't adding keys and values to the dictionary
please help
Your first call to .read() consumes the entire file contents, leaving nothing for the loop to iterate over. Remove it. And that .readline() call does nothing useful. Remove that too.
This should work:
def predator_eat(file):
predator_prey = {}
for line in file:
words = line.split(" ")
predator_prey[words[0]] = words[2]
print(predator_prey)
Cleaning up your code a little:
def predator_eat(f):
predator_prey = {}
for line in f:
rec = line.strip().split(" ")
predator_prey[rec[0]] = rec[2]
return predator_prey
with open(path) as f:
print predator_eat(f)
You basically re declaring python keywords, change file to fileToRead and list something else like totalList or something.

How to populate a dictionary from a file while disregarding the header?

I have this function that's populating itself from another file. I'm having difficulty with avoiding the header of the txt file which begins with ;;; and that the first aspect of the dictionary is supposed to be the words which are in uppercase and the stuff that follows it are the phonemes. I'm not sure which part of my code is wrong :S
def read_file(file):
""" (file open for reading) -> pronunciation dictionary
Read file, which is in the format of the CMU Pronouncing
Dictionary, and return the pronunciation dictionary.
"""
line = file.readline()
while line != ';;;':
line = file.readline()
pronun_dict = {}
line = file.readline()
while line != '':
word = line.isupper()
phoneme = line.strip()
if phoneme not in pronun_dict:
pronun_dict[phoneme] = [word]
line = file.readline()
return pronun_dict
http://gyazo.com/31b414c39cc907bc917f7a1129f4019d
the above link is a screenshot of what the text file looks like!
while line != ';;;': will only be met when the header does not exactly match ';;;'. I assume the header can contain much more data. Try, instead, while line.startswith(';;;'):. As long as this condition is met, the next line in the file will be assigned to variable line. Therefore, your code will loop through that block until a line that doesn't begin with ;;; is found.
http://www.tutorialspoint.com/python/string_startswith.htm

Categories