473,397 Members | 2,033 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,397 software developers and data experts.

Complete noob question, sorry

i am a student working on a project for a python class. i am trying to take one line of a text file at a time and put it into a temp set, then take that temp set and make it a dictionary. then go through the next line so on and so forth. whenever i try to do that it takes the whole file and puts it into a set. im not really sure how to implement my above strategy. right now im just focusing on making everything go through set process. im not yet dealing with the dictionaries. thanks for the help
Mar 7 '08 #1
4 1169
jlm699
314 100+
i am a student working on a project for a python class. i am trying to take one line of a text file at a time and put it into a temp set, then take that temp set and make it a dictionary. then go through the next line so on and so forth. whenever i try to do that it takes the whole file and puts it into a set. im not really sure how to implement my above strategy. right now im just focusing on making everything go through set process. im not yet dealing with the dictionaries. thanks for the help
Can you provide an example of code that you've tried that didn't work?

The most common way to go line by line through a text file (as far as I know) is:
Expand|Select|Wrap|Line Numbers
  1. fh = open('psd_sldr.gm', 'r')
  2. for line in fh:
  3.     print line
  4. fh.close()
  5.  
Mar 7 '08 #2
um yeah.
i got it opening the file and as it goes through the line makes everything lower case and takes out punctuation.

Expand|Select|Wrap|Line Numbers
  1. import string
  2. movieFile = open('movies.txt')
  3. tempSet = set()
  4.  
  5. for lines in movieFile:
  6.     line = line.strip()
  7.     movieList = line.split()
  8.  
  9.     for words in movieList:
  10.         words = words.lower()
  11.         words = words.strip(string.punctuation)
  12.         if words:
  13.             tempSet.add(words)
  14. print tempSet
  15.  
  16. movieFile.close()
  17.  
like i said im just trying right now to get it to read only one line at a time and not the whole file
Mar 7 '08 #3
bvdet
2,851 Expert Mod 2GB
um yeah.
i got it opening the file and as it goes through the line makes everything lower case and takes out punctuation.

Expand|Select|Wrap|Line Numbers
  1. import string
  2. movieFile = open('movies.txt')
  3. tempSet = set()
  4.  
  5. for lines in movieFile:
  6.     line = line.strip()
  7.     movieList = line.split()
  8.  
  9.     for words in movieList:
  10.         words = words.lower()
  11.         words = words.strip(string.punctuation)
  12.         if words:
  13.             tempSet.add(words)
  14. print tempSet
  15.  
  16. movieFile.close()
  17.  
like i said im just trying right now to get it to read only one line at a time and not the whole file
It would be more appropriate to use the variable name 'word' instead of 'words' because you are iterating on a list of words. Also, 'for lines in movieFile:' should be 'for line in movieFile:'. What do you plan to do with the dictionary?
Mar 7 '08 #4
thanks for those tips. i actually found some of my problems with it now. the dictionary is being called in later. the way our project works is we have to take this file of actors and the movies they have played in and set up a dictionary. the key is the movie name with the value being the actors or actresses as a set. we are to prompt the user for two movie names to be called into play that tell us what actors were in the movie, what actors werent, and actors that are common to both. the dictionaries are supposed to make this easier i guess.
Mar 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
3
by: pointBoarder | last post by:
So I have this query that returns ID Item Quantity primary text int unique I have a form with a command button that runs a function, but I need the function to be able...
1
by: you | last post by:
Ok, I beleive that I understand what overriding and overloading do. They both make sense to me. What I don't understand is when they are both used at the same time. Example; Public Overloads...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
2
by: xandeer | last post by:
where is a good open-source project website? thank-you (sorry for being so annoying)(if I'm annoying)(if not then I'm not sorry)
9
by: bjwillykajilly | last post by:
Ok, heres the deal, i'm using netbeans 5.5 to create a Rational calculator(1/2 + 2/3 = 5/6, so on so forth) anyways, I have looked up on google, on this site, everywhere, and I am apperently missing...
5
by: Milan Krejci | last post by:
the thing is that descentant branches i dont want to expand do expand. $id variable contains an array of branches i want the program to go through (alcohol's id -beer id etc) function...
9
by: Ben | last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find a thread that gives the clear example I need. This PC has MySQL and IIS configured and running. The MySQL database is...
3
by: gham | last post by:
I am a complete noob to linux and shell scripting please help this is what I am trying to do 1. Create a script that takes 1 argument being a file, read the inputted file, and look for...
2
by: Carnell, James E | last post by:
I am thinking about purchasing a book, but wanted to make sure I could get through the code that implements what the book is about (Artificial Intelligence a Modern Approach). Anyway, I'm not a...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.