我有一个随机字母,数字和字符的文本文件。在打印过程中,我必须删除特殊字符,最后只使用字母数字字符。在
出于某种原因,我的程序正在打印:
Line read' ,,s.8,ymsw5w-86
'
' ,,s.8,ymsw5w-86
'->' s8ymsw5w86 '
当文本应该只有2行,而不是4行。像这样:
^{pr2}$我不明白怎么了。这是我的代码:
file1=open(textfile1,"r")
for line in file1:
line2="".join(filter(str.isalnum,line))
print("Line read","'",str(line),"'")
print("'",str(line),"'->'",line2,"'")
谢谢。在
文件中的每一行末尾都有一个换行符。如果你不想让它打印出来,就把它剥掉
^{pr2}$
使用f-strings:如果要在字符串中使用单引号,请将字符串括在双引号中,反之亦然
或者
相关问题 更多 >
编程相关推荐