Python Forum
file python task, need help !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file python task, need help !
#1
the task is below:https://pp.userapi.com/c850124/v85012411...C4WFRI.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!
Reply
#2
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply
#6
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.
Reply
#7
#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
Reply
#8
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  simple task in python Rapito 4 2,187 Nov-22-2021, 10:09 PM
Last Post: jefsummers
  small newbie task to python intoxicated_dk 2 2,366 Jun-17-2019, 01:57 PM
Last Post: intoxicated_dk
  Fun Task: Python 3.7.2 CozyDarkness 1 2,041 May-10-2019, 07:12 AM
Last Post: buran
  School python program task help TommyLee 3 4,021 Apr-17-2018, 04:44 AM
Last Post: tannishpage

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020