在 python 中读取文本文件时如何忽略包含字母数字字符的行
how can i ignore a line which contains alphanumeric character while reading a text file in python
我正在阅读 python 中的文本文件,其中主要包含数字行和一些字母数字行。我需要分离奇数和偶数并写入新文件,但是当它进入字母数字行时我得到了错误。请帮忙。
例如....假设txt文件包含以下....2,4,6,10p等。我尝试使用此代码。我希望这段代码在将偶数和奇数写入相应的文件之后工作......它应该说,你的工作已经完成但是这些行保持原样......我们可以这样做吗??
file = open("num.txt","rt")
even = open("even.txt","w+")
odd = open("odd.txt","w+")
for i in file:
if i.strip:
num = int(i)
if (num % 2 == 0):
even.write(str(num))
even.write("\n")
else:
odd.write(str(num))
odd.write("\n")
但是显示如下错误
File "G:/Py_projects/odd_even.py", line 6, in <module>
num = int(i)
ValueError: invalid literal for int() with base 10: '10p\n'
进程已完成,退出代码为 1
请回复。谢谢。
假设您的文本文件如下所示。
test.txt
1
2
3
4
5
6
10p
4
83
456
497987
4564
6589
5493
321654
这应该适合你。
with open('test.txt','r') as f:
even=[]
odd=[]
alphanum=[]
for line in f:
val=line.strip('\n').strip()
if val.isdigit():
if int(val)%2==0:
even.append(int(val))
else:
odd.append(int(val))
else:
alphanum.append(val)
with open('even.txt','w') as f1:
for i in even:
f1.write(str(i)+'\n') #writing even numbers to even.txt file
print(even)
print(odd)
print(alphanum)
根据评论中的建议 AMC 将 val=line.strip('\n').strip()
重写为 val=line.strip('\n ')
您必须对 odd
和 alphanum
执行相同的操作。
输出
[2, 4, 6, 4, 456, 4564, 321654]
[1, 3, 5, 83, 497987, 6589, 5493]
['10p']
with open('file.txt','r') as f:
for line in f:
line = line.strip('\n').strip()
if line.isdigit():
if int(line) % 2 == 0:
with open ('even.txt', 'w') as e:
e.write (line + '\n')
else:
with open ('odd.txt', 'w') as o:
o.write (line + '\n')
elif line.isalnum():
with open ('alphanum.txt', 'w') as a:
a.write (line + '\n')
我正在阅读 python 中的文本文件,其中主要包含数字行和一些字母数字行。我需要分离奇数和偶数并写入新文件,但是当它进入字母数字行时我得到了错误。请帮忙。 例如....假设txt文件包含以下....2,4,6,10p等。我尝试使用此代码。我希望这段代码在将偶数和奇数写入相应的文件之后工作......它应该说,你的工作已经完成但是这些行保持原样......我们可以这样做吗??
file = open("num.txt","rt")
even = open("even.txt","w+")
odd = open("odd.txt","w+")
for i in file:
if i.strip:
num = int(i)
if (num % 2 == 0):
even.write(str(num))
even.write("\n")
else:
odd.write(str(num))
odd.write("\n")
但是显示如下错误
File "G:/Py_projects/odd_even.py", line 6, in <module>
num = int(i)
ValueError: invalid literal for int() with base 10: '10p\n'
进程已完成,退出代码为 1 请回复。谢谢。
假设您的文本文件如下所示。
test.txt
1
2
3
4
5
6
10p
4
83
456
497987
4564
6589
5493
321654
这应该适合你。
with open('test.txt','r') as f:
even=[]
odd=[]
alphanum=[]
for line in f:
val=line.strip('\n').strip()
if val.isdigit():
if int(val)%2==0:
even.append(int(val))
else:
odd.append(int(val))
else:
alphanum.append(val)
with open('even.txt','w') as f1:
for i in even:
f1.write(str(i)+'\n') #writing even numbers to even.txt file
print(even)
print(odd)
print(alphanum)
根据评论中的建议 AMC 将 val=line.strip('\n').strip()
重写为 val=line.strip('\n ')
您必须对 odd
和 alphanum
执行相同的操作。
输出
[2, 4, 6, 4, 456, 4564, 321654]
[1, 3, 5, 83, 497987, 6589, 5493]
['10p']
with open('file.txt','r') as f:
for line in f:
line = line.strip('\n').strip()
if line.isdigit():
if int(line) % 2 == 0:
with open ('even.txt', 'w') as e:
e.write (line + '\n')
else:
with open ('odd.txt', 'w') as o:
o.write (line + '\n')
elif line.isalnum():
with open ('alphanum.txt', 'w') as a:
a.write (line + '\n')