-1

I have some troubles with reading .txt file with numbers, I tried to do it in many ways but still getting error:
if x % 3 == 0: TypeError: not all arguments converted during string formatting

Here are some numbers:

75158, 81917, 2318, 69039, 46112, 30323, 28184, 92597, 89159, 6579, 90155, 
56960, 88247, 72470, 36266, 32693, 31542, 65354, 73315, 1440, 82950, 84901, 
35835, 86530, 27137, 43235, 98977, 21224, 62530, 52675, 18297, 41055, 25597,
13878, 65430, 90050, 66844, 67605

Here is my code:

from string import punctuation


file=open("test.txt","r+")
lines=file.read()
xx=''.join(ch for ch in lines if ch not in punctuation)

print(xx)
for x in xx:
    if x % 3 == 0:
    print(x)

file.close()

I want to get printed all numbers divisible by 3.

While trying to make int from this string there is other error: invalid literal for int() with base 10: '75158....'

Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
Kamzia
  • 11
  • 3
  • tried if int(x) % 3 == 0 ? – Santhosh Kumar Feb 13 '19 at 18:13
  • `x % 3` is modulo if `x` is a number, but [C-style string formatting](https://docs.python.org/3/library/stdtypes.html#old-string-formatting) if `x` is a string. You'll need to split the file content into separate numbers. – jonrsharpe Feb 13 '19 at 18:13
  • You need to convert your input to `int()` before you use the modulo operator. Also, if you plan on on working with CSV files, the `csv` module is very useful and well documented – hyperTrashPanda Feb 13 '19 at 18:13
  • Possible duplicate of [Modulo operator in Python](https://stackoverflow.com/questions/12754680/modulo-operator-in-python) – eagle33322 Feb 13 '19 at 18:16

2 Answers2

1

You could read it and then split on comma , and then do a list comprehension :)

>>> x = "75158, 81917, 2318, 69039, 46112, 30323, 28184, 92597, 89159, 6579, 90155, 56960, 88247, 72470, 36266, 32693, 31542, 65354, 73315, 1440, 82950, 84901, 35835, 86530, 27137, 43235, 98977, 21224, 62530, 52675, 18297, 41055, 25597, 13878, 65430, 90050, 66844, 67605"
>>>
>>> x.strip().split(',') # being careful since reading from a file may have newlines :)
['75158', ' 81917', ' 2318', ' 69039', ' 46112', ' 30323', ' 28184', ' 92597', ' 89159', ' 6579', ' 90155', ' 56960', ' 88247', ' 72470', ' 36266', ' 32693', ' 31542', ' 65354', ' 73315', ' 1440', ' 82950', ' 84901', ' 35835', ' 86530', ' 27137', ' 43235', ' 98977', ' 21224', ' 62530', ' 52675', ' 18297', ' 41055', ' 25597', ' 13878', ' 65430', ' 90050', ' 66844', ' 67605']
>>> [int(y) for y in x.split(',')]
[75158, 81917, 2318, 69039, 46112, 30323, 28184, 92597, 89159, 6579, 90155, 56960, 88247, 72470, 36266, 32693, 31542, 65354, 73315, 1440, 82950, 84901, 35835, 86530, 27137, 43235, 98977, 21224, 62530, 52675, 18297, 41055, 25597, 13878, 65430, 90050, 66844, 67605]
>>> [int(y) for y in x.split(',') if y and int(y) % 3]
[75158, 81917, 2318, 46112, 30323, 28184, 92597, 89159, 90155, 56960, 88247, 72470, 36266, 32693, 65354, 73315, 84901, 86530, 27137, 43235, 98977, 21224, 62530, 52675, 25597, 90050, 66844]

or the right way :)

with open('test.txt') as file_:
    nums = file_.read().strip()
    vals = []
    for val in nums.split(','):
      try:
         val = int(val.strip()) # being again careful for vals like `\n565'
      except ValueError as err:
         continue
      else:
        if val % 3:
          vals.append(val)
print(vals)
han solo
  • 6,390
  • 1
  • 15
  • 19
0

Your reading the numbers as a string and trying use modulo operator. Also since your separating numbers by "," , You have to split it before appending them in a list. Here's the solution:

numbers_file=open("numbers.txt","r")
my_numbers=numbers_file.read().split(',')
print (my_numbers)

for no in my_numbers:
    temp="%s"%(no)
    temp=int(temp)
    if (temp%3 == 0):
        print(no)