473,378 Members | 1,209 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,378 software developers and data experts.

writing files in python

16
Hey there its me again i got this program but i have to improve on it but i am stuck hopefully you can help here is the code
Expand|Select|Wrap|Line Numbers
  1. def student():
  2.     welcome();         
  3.     info = [ ]      
  4.  
  5.     print "Creating a file studentHND.txt to write to: \n";
  6.     output = open("studentHND.txt","w");       
  7.     for i in range(5):          
  8.         name = raw_input('Please enter name: ');
  9.         python = validate('Please enter mark for pythod: ');
  10.         aandd = validate('Please enter mark for aandd: ');
  11.         learn = validate('Please enter mark for learn: ');
  12.         info.append(name)
  13.         info.append(python)
  14.         info.append(aandd)
  15.         info.append(learn)
  16.  
  17.         output.write(str(name)+','),output.write(str(python)+'in PYTHOD,'),output.write(str(aandd)+'in AANDD,'), 'and',output.write(str(learn)+ 'in Learn'),"\n";
  18.  
  19.     print "\nOperation complete"                                                                            
  20.     output.close();
  21.  
  22.  
  23.  
  24.  
  25.  
  26. def validate(prompt):
  27.     limit = input(prompt);
  28.     while(True):
  29.         if (limit>=0  and limit<=100):
  30.             return limit;
  31.         else:
  32.             print limit, "invalid"
  33.             limit = input(prompt);
  34.  
  35.  
  36. def welcome():
  37.     print "This program opens and write information into a file"
  38.     print '*****************************************************************'
  39.  
Basically to improve on it i have to wirte a program which records data from the file created in the above exercise and it should calculate and print out the following:

the name of each student, and which units, if any, they failed(ie a mark under 40). If they failed nothing i should report that as well.

the average score for each unit and which unit achieved the highest average
Thanks
Jun 28 '07 #1
15 2175
bartonc
6,596 Expert 4TB
Hey there its me again i got this program but i have to improve on it but i am stuck hopefully you can help here is the code
Expand|Select|Wrap|Line Numbers
  1. def student():
  2.     welcome();         
  3.     info = [ ]      
  4.  
  5.     print "Creating a file studentHND.txt to write to: \n";
  6.     output = open("studentHND.txt","w");       
  7.     for i in range(5):          
  8.         name = raw_input('Please enter name: ');
  9.         python = validate('Please enter mark for pythod: ');
  10.         aandd = validate('Please enter mark for aandd: ');
  11.         learn = validate('Please enter mark for learn: ');
  12.         info.append(name)
  13.         info.append(python)
  14.         info.append(aandd)
  15.         info.append(learn)
  16.  
  17.         output.write(str(name)+','),output.write(str(python)+'in PYTHOD,'),output.write(str(aandd)+'in AANDD,'), 'and',output.write(str(learn)+ 'in Learn'),"\n";
  18.  
  19.     print "\nOperation complete"                                                                            
  20.     output.close();
  21.  
  22.  
  23.  
  24.  
  25.  
  26. def validate(prompt):
  27.     limit = input(prompt);
  28.     while(True):
  29.         if (limit>=0  and limit<=100):
  30.             return limit;
  31.         else:
  32.             print limit, "invalid"
  33.             limit = input(prompt);
  34.  
  35.  
  36. def welcome():
  37.     print "This program opens and write information into a file"
  38.     print '*****************************************************************'
  39.  
Basically to improve on it i have to wirte a program which records data from the file created in the above exercise and it should calculate and print out the following:

the name of each student, and which units, if any, they failed(ie a mark under 40). If they failed nothing i should report that as well.

the average score for each unit and which unit achieved the highest average
Thanks
There are two things that you can do right away (until someone has some time to look at this):
1) Start using CODE tags in your posts. Instructions are on the right hand side of the page while posting or replying. And since you have over 10 posts, I must tell you that you have been officially warned about using code tags. New site rules mandate a temporary ban on your account for such infractions. All site rules are posted in our Posting Guidelines.

2) Stop putting semicolons at the end of lines (unless you really like looking at them). semicolon is used to separate multiple statements on one line (another discouraged practice).
Jun 28 '07 #2
ghostdog74
511 Expert 256MB
Hey there its me again i got this program but i have to improve on it but i am stuck hopefully you can help here is the code
Expand|Select|Wrap|Line Numbers
  1. def student():
  2.     welcome();         
  3.     info = [ ]      
  4.  
  5.     print "Creating a file studentHND.txt to write to: \n";
  6.     output = open("studentHND.txt","w");       
  7.     for i in range(5):          
  8.         name = raw_input('Please enter name: ');
  9.         python = validate('Please enter mark for pythod: ');
  10.         aandd = validate('Please enter mark for aandd: ');
  11.         learn = validate('Please enter mark for learn: ');
  12.         info.append(name)
  13.         info.append(python)
  14.         info.append(aandd)
  15.         info.append(learn)
  16.  
  17.         output.write(str(name)+','),output.write(str(python)+'in PYTHOD,'),output.write(str(aandd)+'in AANDD,'), 'and',output.write(str(learn)+ 'in Learn'),"\n";
  18.  
  19.     print "\nOperation complete"                                                                            
  20.     output.close();
  21.  
  22.  
  23.  
  24.  
  25.  
  26. def validate(prompt):
  27.     limit = input(prompt);
  28.     while(True):
  29.         if (limit>=0  and limit<=100):
  30.             return limit;
  31.         else:
  32.             print limit, "invalid"
  33.             limit = input(prompt);
  34.  
  35.  
  36. def welcome():
  37.     print "This program opens and write information into a file"
  38.     print '*****************************************************************'
  39.  
Basically to improve on it i have to wirte a program which records data from the file created in the above exercise and it should calculate and print out the following:

the name of each student, and which units, if any, they failed(ie a mark under 40). If they failed nothing i should report that as well.

the average score for each unit and which unit achieved the highest average
Thanks
did you make this program or not? try to do it yourself first. if you are hit with a problem, then ask.
Jun 28 '07 #3
Kasrav
16
I am really sorry its not that i havent read the post guidelines or am not bothered to put codes tags around my post. I have read the guideline posts about two times now and i am still trying to figure how to do it because everytime i try i fail. I have just read the guidelines again this morning and i am still a bit confused as to do it may be you can enlight me more. I apologise for anything wrong done.
Thanks
Jun 29 '07 #4
bvdet
2,851 Expert Mod 2GB
I am really sorry its not that i havent read the post guidelines or am not bothered to put codes tags around my post. I have read the guideline posts about two times now and i am still trying to figure how to do it because everytime i try i fail. I have just read the guidelines again this morning and i am still a bit confused as to do it may be you can enlight me more. I apologise for anything wrong done.
Thanks
Kasrav - This is an opening code tag for Python: [code=Python]

This is a closing code tag: [/c o d e]

I added spaces in between the characters so the tags will display.

Could you show us a complete sample data file? That would help us help you.
Jun 29 '07 #5
bartonc
6,596 Expert 4TB
I am really sorry its not that i havent read the post guidelines or am not bothered to put codes tags around my post. I have read the guideline posts about two times now and i am still trying to figure how to do it because everytime i try i fail. I have just read the guidelines again this morning and i am still a bit confused as to do it may be you can enlight me more. I apologise for anything wrong done.
Thanks
OK, you don't have to worry about being banned for this. It's nice to know that you are trying. It's OK if you make a mistake, too, because I can fix those. You also have 5 minutes to edit your post and there is a Preview Post button so you can see what it will look like before you Submit it. Also, the editor where you put you post in has all the controls you need to make the all kinds of tags. Make sure that "Enhanced Mode" is selected. The # icon makes code tags.

It's a learning thing; just keep trying. Thanks,
Barton
Jun 29 '07 #6
Kasrav
16
Expand|Select|Wrap|Line Numbers
  1. def student():
  2.     welcome();  
  3.     info = [ ]  
  4.  
  5.     print "Creating a file studentHND.txt to write to: \n";
  6.     output = open("studentHND.txt","w");  
  7.     for i in range(5):          
  8.         name = raw_input('Please enter name: ');
  9.         python = validate('Please enter mark for pythod: ');
  10.         aandd = validate('Please enter mark for aandd: ');
  11.         learn = validate('Please enter mark for learn: ');
  12.         info.append(name)
  13.         info.append(python)
  14.         info.append(aandd)
  15.         info.append(learn)
  16.  
  17.         output.write(str(name)+','),output.write(str(python)+'in PYTHOD,'),output.write(str(aandd)+'in AANDD,'), 'and',output.write(str(learn)+ 'in Learn'),"\n";
  18.  
  19.     print "\nOperation complete"                                                                            
  20.     output.close();
  21.  
  22.  
  23.  
  24.  
  25. def validate(prompt):
  26.     limit = input(prompt);
  27.     while(True):
  28.         if (limit>=0  and limit<=100):
  29.             return limit;
  30.         else:
  31.             print limit, "invalid 
  32.             limit = input(prompt);
  33.  
  34. def welcome():
  35.     print "This program opens and write information into a file"
  36.     print '*****************************************************************'
  37.  
  38.  
  39. student()
  40.  
this one i have tried to put the tags on in but i am confused as to how you make it look like a proper python document
Jun 30 '07 #7
bartonc
6,596 Expert 4TB
Great job! I've made the change and left a "reason for editing" at the bottom that explains it. It's hard to explain in a post because the parser eats up properly formatted tags.
Jun 30 '07 #8
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. def student():
  2.     welcome();  
  3.     info = [ ]  
  4.  
  5.     print "Creating a file studentHND.txt to write to: \n";
  6.     output = open("studentHND.txt","w");  
  7.     for i in range(5):          
  8.         name = raw_input('Please enter name: ');
  9.         python = validate('Please enter mark for pythod: ');
  10.         aandd = validate('Please enter mark for aandd: ');
  11.         learn = validate('Please enter mark for learn: ');
  12.         info.append(name)
  13.         info.append(python)
  14.         info.append(aandd)
  15.         info.append(learn)
  16.  
  17.         output.write(str(name)+','),output.write(str(python)+'in PYTHOD,'),output.write(str(aandd)+'in AANDD,'), 'and',output.write(str(learn)+ 'in Learn'),"\n";
  18.  
  19.     print "\nOperation complete"                                                                            
  20.     output.close();
  21.  
  22.  
  23.  
  24.  
  25. def validate(prompt):
  26.     limit = input(prompt);
  27.     while(True):
  28.         if (limit>=0  and limit<=100):
  29.             return limit;
  30.         else:
  31.             print limit, "invalid 
  32.             limit = input(prompt);
  33.  
  34. def welcome():
  35.     print "This program opens and write information into a file"
  36.     print '*****************************************************************'
  37.  
  38.  
  39. student()
  40.  
But it looks like there are some bugs in the parser at the moment. I'm posting a copy to see the differences...

Definitely some thing wrong on our end. I'll report it and try to get it fixed.
Jun 30 '07 #9
Kasrav
16
hey there, sorry i took long to reply its just that i dnt have regular use of the net.
I just wanted to ask if i am going to get some help on this file problem, thanks

<Mod EDIT: pasting last submission here>
[Sorry, I though you were presenting working code because you did not explain any problem that you were having.]
Expand|Select|Wrap|Line Numbers
  1. def student():
  2.     welcome();  
  3.     info = [ ]  
  4.  
  5.     print "Creating a file studentHND.txt to write to: \n";
  6.     output = open("studentHND.txt","w");  
  7.     for i in range(5):          
  8.         name = raw_input('Please enter name: ');
  9.         python = validate('Please enter mark for pythod: ');
  10.         aandd = validate('Please enter mark for aandd: ');
  11.         learn = validate('Please enter mark for learn: ');
  12.         info.append(name)
  13.         info.append(python)
  14.         info.append(aandd)
  15.         info.append(learn)
  16.  
  17.         output.write(str(name) + ','),output.write(str(python) + 'in PYTHOD,'),output.write(str(aandd)+'in AANDD,'), 'and',output.write(str(learn)+ 'in Learn'),"\n";
  18.  
  19.     print "\nOperation complete"                                                                            
  20.     output.close();
  21.  
  22.  
  23.  
  24.  
  25. def validate(prompt):
  26.     limit = input(prompt);
  27.     while(True):
  28.         if (limit>=0  and limit<=100):
  29.             return limit;
  30.         else:
  31.             print limit, "invalid 
  32.             limit = input(prompt);
  33.  
  34. def welcome():
  35.     print "This program opens and write information into a file"
  36.     print '*****************************************************************'
  37.  
  38.  
  39. student()
  40.  
Jul 5 '07 #10
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. def student():
  2.     welcome();  
  3.     info = [ ]  
  4.  
  5.     print "Creating a file studentHND.txt to write to: \n"
  6.     output = open("studentHND.txt","w")
  7.     for i in range(5):          
  8.         name = raw_input('Please enter name: ')
  9.         python = validate('Please enter mark for pythod: ')
  10.         aandd = validate('Please enter mark for aandd: ')
  11.         learn = validate('Please enter mark for learn: ')
  12.         info.append(name)
  13.         info.append(python)
  14.         info.append(aandd)
  15.         info.append(learn)
  16.         # I guess that you thought that you could use commas that way that print does; You can't.
  17.         # I've added spaces where strings were followed by commas.
  18.         output.write(str(name) + ', ')
  19.         output.write(str(python) + 'in PYTHOD, ' )
  20.         output.write(str(aandd) + 'in AANDD,' + 'and ')
  21.         output.write(str(learn)+ 'in Learn\n')
  22.  
  23.     print "\nOperation complete"                                                                            
  24.     output.close()
  25. # I also remove all those extra semicolons; they're for separating multiple statements on one line.
  26.  
  27.  
  28.  
  29.  
  30. def validate(prompt):
  31.     limit = input(prompt);
  32.     while(True):
  33.         if (limit>=0  and limit<=100):
  34.             return limit;
  35.         else:
  36.             print limit, "invalid" # added closing quotes.
  37.             limit = input(prompt);
  38.  
  39. def welcome():
  40.     print "This program opens and write information into a file"
  41.     print '*****************************************************************'
  42.  
  43.  
  44. student()
  45.  
Jul 5 '07 #11
Kasrav
16
sorry Barton i dint expalin myself well the program i posted is ok but the problem that i have is that i have to use it to write another program which records data from the file created(program) and it should calculate and print out the following:

the name of each student, and which units, if any, they failed(ie a mark under 40). If they failed nothing i should report that as well.

the average score for each unit and which unit achieved the highest average

hope that makes my problem clear, thanks
Jul 6 '07 #12
bartonc
6,596 Expert 4TB
sorry Barton i dint expalin myself well the program i posted is ok but the problem that i have is that i have to use it to write another program which records data from the file created(program) and it should calculate and print out the following:

the name of each student, and which units, if any, they failed(ie a mark under 40). If they failed nothing i should report that as well.

the average score for each unit and which unit achieved the highest average

hope that makes my problem clear, thanks
Here's the direction I think that you should go in order to create such a program:
1) Don't worry about storing in a text file just yet.
2) Extend your idea of use the info list (info.append(data)) to include the ability to retrieve the desired data and precess it.
2.5) Use paper and pencil to draw your data structure:
.. a list .. [of students]
.. a student.. {'ID':1, 'name':'barton', 'python':1000}
(that sort of thing).
3) Once you have a program that can put more than one record into a list in memory and get the data out for display - maybe consider a list of dictionaries for your database structure -, then we'll work on saving that data to a file and retrieving it.
Jul 6 '07 #13
bvdet
2,851 Expert Mod 2GB
You should carefully consider Barton's suggestions since they are good ones. To help you with your problem, I would start by modifying your code a bit to get the output in a simpler form:
Expand|Select|Wrap|Line Numbers
  1. def student():
  2.     welcome()
  3.  
  4.     print "Creating a file studentHND.txt to write to: \n"
  5.     output = open(r"H:\TEMP\temsys\studentHND1.txt","w")
  6.     for i in range(5):          
  7.         name = raw_input('Please enter name: ')
  8.         python = validate('Please enter mark for pythod: ')
  9.         aandd = validate('Please enter mark for aandd: ')
  10.         learn = validate('Please enter mark for learn: ')
  11.  
  12.         output.write(','.join([name, python, aandd, learn])+'\n')
  13.  
  14.     print "\nOperation complete"                             
  15.     output.close()
  16.  
  17. def validate(prompt):
  18.     limit = int(raw_input(prompt))
  19.     while(True):
  20.         if (limit>=0  and limit<=100):
  21.             return str(limit)
  22.         else:
  23.             print limit, "invalid"
  24.             limit = raw_input(prompt)
  25.  
  26. def welcome():
  27.     print "This program opens and write information into a file"
  28.     print '*****************************************************************'
  29.  
  30. student()
The data file will look like this:

Jeff Smith, 45, 80, 60
Bill Jones, 60, 90, 80
Cindy Hill, 90, 99, 78
Andy Miller, 39, 99, 80
Fred Taylor, 45, 66, 77

The following code will read the data file and create a dictionary in memory:
Expand|Select|Wrap|Line Numbers
  1. # read data file and calculate statistics
  2. # Unit hash table
  3. unitDict = {0: 'pythod', 1: 'aandd', 2: 'learn'}
  4.  
  5. fn = r'H:\TEMP\temsys\studentHND.txt'
  6.  
  7. lineList = [item.strip().split(',') for item in open(fn).readlines() if item != '\n']
  8.  
  9. # Name list
  10. keys = [i[0] for i in lineList]
  11. # Scores list
  12. values = [[int(s) for s in item] for item in [j[1:] for j in lineList]]
  13.  
  14. # Create a dictionary - Names are dictionary keys, score lists are the values
  15. dataDict = dict(zip(keys, values))
The dictionary looks like this:
Expand|Select|Wrap|Line Numbers
  1. >>> dataDict
  2. {'Jeff Smith': [45, 80, 60], 'Andy Miller': [39, 99, 80], 'Cindy Hill': [90, 99, 78], 'Fred Taylor': [45, 66, 77], 'Bill Jones': [60, 90, 80]}
To access individual elements:
Expand|Select|Wrap|Line Numbers
  1. >>> dataDict['Jeff Smith'][0]
  2. 45
  3. >>> unitDict[0]
  4. 'pythod'
  5. >>> 
Here's a little interaction:
Expand|Select|Wrap|Line Numbers
  1. >>> for name in dataDict:
  2. ...     for i, score in enumerate(dataDict[name]):
  3. ...         print "%s %s %d" % (name, unitDict[i], score)
  4. ...         
  5. Jeff Smith pythod 45
  6. Jeff Smith aandd 80
  7. Jeff Smith learn 60
  8. Andy Miller pythod 39
  9. Andy Miller aandd 99
  10. Andy Miller learn 80
  11. Cindy Hill pythod 90
  12. Cindy Hill aandd 99
  13. Cindy Hill learn 78
  14. Fred Taylor pythod 45
  15. Fred Taylor aandd 66
  16. Fred Taylor learn 77
  17. Bill Jones pythod 60
  18. Bill Jones aandd 90
  19. Bill Jones learn 80
  20. >>> 
From this you can determine the required statistics. We could do this for you, but it would be best if you could pick it up from here.
Jul 6 '07 #14
Kasrav
16
thanks once again u guys
Jul 9 '07 #15
bartonc
6,596 Expert 4TB
thanks once again u guys
Any time, really .
Jul 9 '07 #16

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

Similar topics

4
by: John Pote | last post by:
Hello, help/advice appreciated. Background: I am writing some web scripts in python to receive small amounts of data from remote sensors and store the data in a file. 50 to 100 bytes every 5 or...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
3
by: koutoo | last post by:
I have a code that writes to 2 seperate files. I keep getting a "list index out of range" error. The strange part is that when checking the files that I'm writing too, the script has already...
20
by: Marin Brkic | last post by:
Hello all, please, let me apologize in advance. English is not my first language (not even my second one), so excuse any errors with which I'm about to embarass myself in front of the general...
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: 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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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...

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.