C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Python - read numbers from text file in a function

By : user2953100
Date : November 19 2020, 03:59 PM
To fix this issue I'm trying to write a program that ensures to write on a text file finding prime numbers between given number and limit. If the number exists in file, It tries to write another number which is coprime with the given(entered) number. Then it is written on the text file. My problem is checking numbers from text file whether exist or not. How can I write it ? So, I have researched since the morning but, I can't find helpful answers for the problem. I think Python quite a change works from C. , presuming all nums are all on separate lines like below:
code :
``````1
5
7

from fractions import gcd

def coprime(n1, n2):
return gcd(n1, n2) == 1 # same as if gcd(n1, n2) == 1:return True else: return False

with open("out.txt","a+") as f: # with automatically closes your files
# add all previous numbers into a set, casting to int using map
# map(int, f)  equivalent to [int(ele) for ele in f] in python2
nums_set = set(map(int, f)) # with 1 5 and 7 in the file nums_set = set([1, 5, 7])
i = 0
j = 0

num = int(raw_input("Please enter number "))
limit = int(raw_input("Please enter limit "))

while i < limit:
if j not in nums_set and coprime(num,j):
f.write("{}\n".format(j))
print "{} is written on the text file.".format(j)
i += 1
j += 1
# add current j's from this run to the set to avoid reopening and rechecking he file
else:
j += 1
``````

Share :

## How to read numbers in text file using python?

By : Busy Buzzy Bee
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am new to python programming and I am learning python by doing simple programs. Here is what I would like to do: if I have a text file containing numbers: say this a f1.txt , You could do it like this:
code :
``````a1 = []
a2 = []
a3 = []
a4 = []

with open('f1.txt') as f:
for line in f:
data = line.split()
a1.append(int(data[0]))
a2.append(int(data[1]))
a3.append(int(data[2]))
a4.append(int(data[3]))
``````
``````a = [[], [], [], []] # A list with four empty lists
``````
``````for i, value in enumerate(line.split()):
a[i].append(int(value))
``````

## read numbers from text file python

By : user2592799
Date : March 29 2020, 07:55 AM
To fix the issue you can do I am trying to read a text file and return the contents of the text file. The textfile contains a matrix. When i run my code with the file it just prints the first line. My code looks right and i have searched online and cant seem to find the problem.
code :
``````while line != "":
return line # function ends
``````
``````while line != "":
print line
``````

## How to read numbers in text file as numbers in to list in python?

By : joe458
Date : March 29 2020, 07:55 AM
help you fix your problem I have a text file in which there are numbers in each line(only numbers).it my color.txt look like , just cast your strings into integers:
code :
``````g_colour_list.append(int(line.strip('\n')))
``````

## How to read numbers in a text file for python?

By : dbf30
Date : March 29 2020, 07:55 AM
To fix the issue you can do I'm writing a program where I'm doing simple calculations from numbers stored in a file. However, it keeps on returning a ValueError. Is there something I should change in the code or how the text file is written? , If you can't read a line, skip to the next one.
code :
``````for line in in_file:
try:
donation = float(line)
except ValueError:
continue
``````
``````with open("donations.txt", "r") as in_file:
highest = lowest = donation = None
number = total = 0
for line in in_file:
try:
donation = float(line)
except ValueError:
continue
if highest is None or donation > highest:
highest = donation

if lowest is None or donation < lowest:
lowest = donation

number += 1
total += donation

average = total / number

print "The highest amount is \$%.2f" %highest
print "The lowest amount is \$%.2f" %lowest
print "The total donation is \$%.2f" %total
print "The average is \$%.2f" %average
``````

## Can't read numbers from a text file in python

By : bnu
Date : March 29 2020, 07:55 AM
like below fixes the issue So this is just one way to take all of your values and calculate the average.
code :
``````input_file = input("enter file name: ")
open_file = open(input_file, 'r')
``````
``````grades_list = []
``````
``````grade_convert = []    #new list for all of the converted values
convert = float(grade)     #convert each value in the list into a float
``````grade_convert = sorted(grade_convert)    #very first element will be the lowest