# This program asks for an input and output file, and uses mult. and add.
# to combine files
def main():
print("This program creates a file of numbers from a")
print("file of numbers.")
infileName = input("What file are the numbers in? ")
outfileName = input("What file should the numbers go in? ")
infile = open(infileName, 'r')
outfile = open(outfileName, 'w')
for line in infile:
nums = line.split()
nums = float()
first = nums[0]
second = nums[-1]
newfirst = (first + second)
newsecond = (first * second)
print(newfirst, newsecond, file=outfile)
infile.close()
outfile.close()
print("Numbers have been written to", outfileName)
main()
the input txt file looks like this:
4.5 16.8
15 25
150 220.4
5.8 90
-1 -2
6.8 -2.3
this is what i have so far, I know everything is right other than the part where I split the input file. I just want to know how to convert the numbers in the input file to floats so i can add them and multiply them
You should replace this:
nums = line.split()
nums = float()
first = nums[0]
second = nums[-1]
with this:
nums = line.split()
first = float(nums[0])
second = float(nums[-1])
or this:
nums = [float(x) for x in line.split()]
first = nums[0]
second = nums[-1]
Related
I am trying to get an average from a text file that uses a def function. I am trying to convert the list from the text file to int(). Instead of converting it gives me the error: " ValueError: invalid literal for int() with base 10: '5, 5, 6, 7' ". The "5, 5, 6, 7" is one that I made from the proper .txt file. Here is the code:
def getNumberList(filename):
with open(filename,'r') as f:
lyst = f.read().split('\n')
numberList = [int(num) for num in lyst]
return numberList
def getAverage(filename, func):
numbers = func(filename)
return sum(numbers)/len(numbers)
def main():
filename = input("Input the file name: ")
average = getAverage(filename, getNumberList)
print(average)
if __name__ == "__main__":
main()
You are splitting by line but you are not splitting by commas, so you are trying to convert 5,5,6,7 to an integer, which is impossible. You need to also split by commas after you split by line, and then combine them into one list, if you want to average all the numbers in the file. The following should work:
def getNumberList(filename):
with open(filename,'r') as f:
lines = f.readlines()
numberList = [int(num) for num in line.split(',') for line in lines]
return numberList
Looks like you might need to split each element with lyst using "," because right now it is trying to convert each line which has "1,2,3" as input.
So, change this and try.
def getNumberList(filename):
with open(filename,'r') as f:
lyst = []
temp = f.read().strip().split('\n')
for i in temp:
lyst += i.strip().split(',')
numberList = [int(num) for num in lyst]
return numberList
Currently learning python, what I need to do is set the limit of a for loop equal to the int version of the first item read in a file
for example:
if a file contains the list:
10
1
2
3
4
...
I want the first line which contains 10, as the end limit for a for loop.
or maybe there is another way to accomplish this, that would also be appreciated.
here is what i have code:
otherFile = input("Do you have a file to open? (Y/N): ")
while(otherFile == 'Y' or otherFile == 'y'):
totalSum = 0
try:
with open(input("which file would you like to open? "), "r") as file:
for line in file:
totalSum += int(line)
print(line, end="")
print()
print("Total Sum is: ", totalSum)
except FileNotFoundError:
print("File name invalid, Please enter valid name.")
print()
except ValueError:
print("Invalid data type within file.")
otherFile = input("Do you have another file to open? (Y/N): ")
print("Goodbye")
I tried to read only the first line and turning that into an int, then setting it to limit but it did not work, i show that below:
with open(input("which file would you like to open? "), "r") as file:
numLines = int(file.readline())
for line in numLines: <---------- #what I tried
totalSum += int(line)
print(line, end="")
print()
print("Total Sum is: ", totalSum)
Do not cast string into int if you need a list you may create an integer list like this
integers="10 20 30 40"
integers = list(map(int,integers.split(" ")))
Following code can be useful for setting the limit for range
x=fh.readline()
limit_to_set=int(x.split(" ")[0])
for i in range(0,limit_to_set):
# perform your operation
# just showing what is in this file
>>> with open('file_1', 'r') as f:
... for line in f:
... print(line.rstrip())
...
2
abc
def
ghi
jkl
# print only 2 lines
>>> with open('file_1', 'r') as f:
... for _ in range(int(next(f))):
... print(next(f).rstrip())
...
abc
def
If you do it this way and there is a file where the number in the first line is greater than the total number of lines, you will have to handle the StopIteration on your own.
One way would be to write a code like this:
import pandas as pd
df = pd.read_csv('test.txt', delimiter = ' ')
num = df.columns[0]
num here is the integer you are seeking, than you can run your loop like this:
for i in range(num):
when dealing with integers you should use range()
I need to write a program, in Python, that looks at a list of numbers in a separate text file, and does the following: Displays all the numbers from the file, adds the total of all the numbers, tells me how many numbers are in the file. My problem is that it skips the first number in file
Here's the code for the program that's writing to the file, if that helps at all:
import random
amount = int (input ('How many random numbers do you want in the file? '))
infile = open ('random_numbers.txt', 'w')
for x in range (amount):
numbers = random.randint (1, 500)
infile.write (str (numbers) + '\n')
infile.close()
And here's my code for the reading the numbers in the file:
amount = 0
total = 0
infile = open ('random_numbers.txt', 'r')
numbers = (infile.readline())
try:
while numbers:
numbers = (infile.readline())
numbers = numbers.strip('\n')
numbers = int (numbers)
print (numbers)
total += numbers
amount += 1
except ValueError:
pass
print ('')
print ('')
amount +=1
print ('Your total is: ' ,total)
print ('The amount of numbers in file is: ', amount)
Now my issue is that it skips over the first number in the file. I first noticed it wasnt giving me the correct amount of numbers, hence the additional statement to add an additional 1 to the amount variable. But then I tested again and noticed it was skipping the first number in file.
How about:
with open('random_numbers.txt', 'r') as f:
numbers = map(lambda x: int(x.rstrip()), f.readlines())
This strips any trailing newline characters from the line in the string and then casts it as an int. It also closes the file when done with it.
I'm not sure why you'd want to count how many times it loops, but if that's what you want to do, you can do it this way:
numbers = list()
with open('random_numbers.txt', 'r') as f:
counter = 0
for line in f.readlines():
try:
numbers.append(int(line.rstrip()))
except ValueError: # Just in case line can't be converted to int
pass
counter += 1
I would just use len(numbers) with the result of the first method, though.
As ksai mentioned, the ValueError is coming up, most likely, because of the \n at the end of the line. I've added an example of catching the ValueError with a try/except just in case it comes across a line that's not convertible to a number for some reason.
Here's the code successfully running in my shell:
In [48]: import random
...:
...: amount = int (input ('How many random numbers do you want in the file?
...: '))
...: infile = open ('random_numbers.txt', 'w')
...: for x in range (amount):
...: numbers = random.randint (1, 500)
...: infile.write (str (numbers) + '\n')
...: infile.close()
...:
How many random numbers do you want in the file? 5
In [49]: with open('random_numbers.txt', 'r') as f:
...: numbers = f.readlines()
...: numbers = map(lambda x: int(x.rstrip()), numbers)
...:
In [50]: numbers
Out[50]: <map at 0x7f65f996b4e0>
In [51]: list(numbers)
Out[51]: [390, 363, 117, 441, 323]
Assuming, as in your code that generates these numbers, that the contents of 'random_numbers.txt' are integers separated by newlines:
with open('random_numbers.txt', 'r') as f:
numbers = [int(line) for line in f.readlines()]
total = sum(numbers)
numOfNums = len(numbers)
'numbers' contains all the numbers from the file in a list. You can print this or print(','.join(map(str,numbers))) if you don't want the square brackets.
'total' is their sum
'numOfNums' is how many numbers were in the file.
What ended up working for me was this:
amount = 0
total = 0
infile = open ('random_numbers.txt', 'r')
numbers = (infile.readline())
try:
while numbers:
numbers = (infile.readline())
numbers = numbers.strip('\n')
numbers = int (numbers)
print (numbers)
total += numbers
amount += 1
except ValueError:
pass
print ('')
print ('')
amount +=1
print ('Your total is: ' ,total)
print ('The amount of numbers in file is: ', amount)
Cory's tip on adding a try and except is what I believe ended up doing the trick.
If me, I would like to code like this:
from random import randint
fname = 'random_numbers.txt'
amount = int(input('How many random numbers do you want in the file? '))
with open(fname, 'w') as f:
f.write('\n'.join([str(randint(1, 500)) for _ in range(amount)]))
with open(fname) as f:
s = f.read().strip()
numbers = [int(i) for i in s.split('\n') if i.isdigit()]
print(numbers)
Or like this (need to pip install numpy):
import numpy as np
from random import randint
fname = 'random_numbers.txt'
amount = int(input('How many random numbers do you want in the file? '))
np.array([randint(1, 500) for _ in range(amount)]).tofile(fname)
numbers = np.fromfile(fname, dtype='int').tolist()
print(numbers)
I think the issue is how you placed the code as you unintentionally skipped the first row with another call of infile.readline()
amount = 0
total = 0
infile = open ('random_numbers.txt', 'r')
numbers = (infile.readline())
try:
while numbers:
numbers = numbers.strip('\n')
numbers = int (numbers)
print (numbers)
total += numbers
amount += 1
numbers = (infile.readline()) #Move the callback here.
except ValueError:
raise ValueError
print ('')
print ('')
# The amount should be correct already, no need to increment by 1.
# amount +=1
print ('Your total is: ' ,total)
print ('The amount of numbers in file is: ', amount)
Works fine for me.
I'm creating a function that reads a .txt file that contains the following:
Testing.txt:
a 1 34
b 2 25
c 3 23
j 4 80
I need to create a function that reads this file and prints the average of the last 2 digits of each line in the file.
What I have so far is this:
def Stats():
filename = input("Enter a file name: ")
with open(filename) as f:
data = [int(line) for line in f]
print("The average value is ", sum(data)/len(data))
Stats()
Pros:
this function will produce the average of int values within a function.
cons:
It will not work if the file contains str within it.
Question: How can I get my function to work with a file such as the one listed above called (testing.txt)?
I think this is what you are after:
# filename = input("Enter a file name: ")
filename = "input.txt"
def avg_file(filename):
with open(filename, 'r') as f:
data = [int(line.split()[2]) for line in f]
return sum(data)/len(data)
avg = avg_file(filename)
print("The average value is: ", avg)
# The average value is: 40.5
the key is this line.split()[2]. This splits a line based on spaces, and takes 3th value.
You could ignore non-numbers this way:
data = [int(token) for token in f.read().split() if token.isdigit()]
What about something like:
import csv
def mean(lst): return sum(lst) / len(lst)
with open('input.txt') as f:
reader = csv.reader(f, delimiter=' ')
lines = [line for line in reader]
col3 = [int(line[2]) for line in lines]
print(mean(col3)) # 40.5
This just uses the csv module to parse the file lines into rows containing elements. You could accomplish the same thing with something like:
def mean(lst): return sum(lst) / float(len(lst))
with open('input.txt') as f:
lines = [line.split() for line in f]
col3 = [int(line[2]) for line in lines]
print mean(col3) # 40.5
Something like this:
data = [sum(map(int, line.split(' ')[1:])) / 2 for line in f]
Or something more old-school, in case you have unknown mix of string and int values:
data = []
for line in f:
integers = []
for segment in line.split():
try:
integers.append(int(segment))
except ValueError:
pass
data.append(sum(integers) / len(integers))
def make_number_list(a_file):
number_list= []
for line_str in a_file:
line_list = line_str.split()
for number in line_list:
if number != " ":
number_list.append(number)
return number_list
opened_file = open(input("Name of input file: "))
a_file_list = make_number_list(opened_file)
print(a_file_list)
print("Length: ", len(a_file_list))
I am trying to read (eventually) 1000 integer values from a file into a list.. then find their max, min and i th value. However, this is not working to read the list (I'm just using a test list which is a file in TextEdit and is a bunch of random numbers separated by a single white space). Any suggestions?
# assumes Python 3.x
def read_nums(fname):
with open(fname) as inf:
return [int(i) for i in inf.read().split()]
def main():
fname = input("Name of input file: ")
nums = read_nums(fname)
print("Read {} numbers".format(len(nums)))
if __name__=="__main__":
main()
I am not sure how the numbers are present in your input file. Nonetheless, there are a few changes you could do to your code:
Move the return statement outside the loop.
A better way of checking if number exists would be:
if number.isdigit():
Which gives you:
def make_number_list(a_file):
number_list= []
for line_str in a_file:
line_list = line_str.split()
for number in line_list:
if number.isdigit():
number_list.append(number)
return number_list