Python Forum
file python task, need help ! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: file python task, need help ! (/thread-17549.html)



file python task, need help ! - rayabv - Apr-15-2019

the task is below:https://pp.userapi.com/c850124/v850124111/11a750/yRoZ2C4WFRI.jpg

Ask user for a file with integer numbers (one number in each row). if the file does not exist, print a
message and finish.

Make an empty list. Read each row, delete the new line character (method strip()), convert it to
integer and append into the list. If the row does not contain an integer, skip it and continue with next.

Print the greatest number or a message for an empty list.

Write all the numbers and their difference from the greatest number to a new .csv file in the form
number;difference

Example
Input file:
87
-18
-100
-55
15
-2
-88
90
11
21
73
-14
-38
Output file:
87;3
-18;108
-100;190
-55;145
15;75
-2;92
-88;178
90;0
11;79
21;69
73;17
-14;104
-38;128
will be very greatful for each answer!


RE: file python task, need help ! - perfringo - Apr-15-2019

I think that instructions are very straightforward, almost all steps are listed and also instructions how to handle some edge-cases.

Translate this into Python. If some parts of your code doesn't work as expected, ask advice.


RE: file python task, need help ! - rayabv - Apr-15-2019

Actually, my output data is no divided and is written in line and that is wrong.

After that, I can not understand how to find integers in list(can suppose it is isdigit function but how to make a block with it?)

Moreover, I am not sure about saving in csv format

It seems to be complicated to write a code with numerous conditions and blocks, that is why I have asked about the task.


RE: file python task, need help ! - ichabod801 - Apr-15-2019

And we still want to see your attempt at coding it. Give it a try, post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.


RE: file python task, need help ! - rayabv - Apr-15-2019

Here we are. After dealing with the first steps [Image: RjP5ZcYbTa0.jpg]

I understood that list is not divided.
i also tried to use isdigit function, but could not understand how to make a block.

Max function is essential to use while dealing with the biggest value of the list.
I am not convince if it will work if I put it after the TRY and EXCEPT block


RE: file python task, need help ! - nilamo - Apr-15-2019

Code is text. When sharing code, please share the code. Taking a screenshot of it turns that beautiful text into an awful image. And it's so much more work for you, than simply copy/pasting the code lol.


RE: file python task, need help ! - rayabv - Apr-15-2019

#ask for a file
import csv
userinput = input('Enter a file name containing integer numbers that you want to read:  ')
myfile = userinput
#read if exist

try:
    myfile = open(userinput)
except:
    print('a file does not exist')
    exit()
#modifications with our file
myfile = open(userinput)

rows = []
for row in myfile:
    row=int(row.strip())
    rows.append(row)

if len(rows)==0:
    print('there is no integers')
    input()
    exit()

a=max(rows)
print('maximum  ',a)

myfile = open('output.csv','w')
for row in myfile:
myfile.write(str(row)+';'+str(a-row)+'\n')
i have tried a lot..still does not work


RE: file python task, need help ! - Yoriz - Apr-15-2019

I've gone through the code and commented changes
#ask for a file
# import csv # not used
userinput = input('Enter a file name containing integer numbers that you want'
                  ' to read:  ')
myfile = userinput
#read if exist
 
try:
    myfile = open(userinput)
except:
    print('a file does not exist')
    exit()
#modifications with our file
# myfile = open(userinput) # don't need to re-open the file
 
rows = []
for row in myfile:
    row=int(row.strip())
    rows.append(row)
 
myfile.close() # close the file
 
if len(rows)==0:
    print('there is no integers')
#     input() # not needed
    exit()
 
a=max(rows)
print('maximum  ',a)
 
myfile = open('output.csv','w')
for row in rows: # for row in myfile should be going through rows
    myfile.write(str(row)+';'+str(a-row)+'\n') # indented this line

myfile.close() # close the file