简体   繁体   English

Python3从.txt文件中读取带有逗号的数字

[英]Python3 reading numbers from .txt file with commas

I have some troubles with reading .txt file with numbers, I tried to do it in many ways but still getting error: 我在读取带有数字的.txt文件时遇到了一些麻烦,我试图通过多种方式进行操作,但仍然出现错误:
if x % 3 == 0: TypeError: not all arguments converted during string formatting 如果x%3 == 0:TypeError:不是在格式化字符串时转换了所有参数

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. 我想打印所有可被3整除的数字。

While trying to make int from this string there is other error: invalid literal for int() with base 10: '75158....' 尝试从此字符串进行int转换时,还有其他错误:int()以10为基数的无效文字:“ 75158 ....”

You could read it and then split on comma , and then do a list comprehension :) 您可以阅读它,然后以逗号split ,然后进行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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM