|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
文件a和b功能如下:
终端接收当前目录下两个txt文件,将第一个文件的单词统计写入第二个txt文件中
例:
终端输入
- python a.py a1.txt a2.txt
复制代码
但是结果是a 只能写入第一行,b成功运行全部统计
用vscode对比过ab两个文件代码无任何区别,连空格换行都是一致
以下是a代码
- import sys
- import re
- def countFile(filename,words):
- try:
- f = open(filename,"r",encoding = "gbk" )
- except Exception as e:
- print(e)
- return 0
- txt = f.read()
- f.close()
- splitChars = set([])
- for c in txt:
- if not ( c >= 'a' and c <= 'z' or c >= 'A' and c <= 'Z'):
- splitChars.add(c)
- splitStr = ""
- for c in splitChars:
- if c in {'.','?','!','"',"'",'(',')','|','*','$','\\','[',']','^','{','}'}:
- splitStr += "\" + c + "|"
- else:
- splitStr += c + "|"
- splitStr += " "
- lst = re.split(splitStr,txt)
- for x in lst:
- if x == "":
- continue
- lx = x.lower()
- if lx in words:
- words[lx] += 1
- else:
- words[lx] = 1
- return 1
- result = {}
- if countFile(sys.argv[1],result) ==0:
- exit()
- lst = list(result.items())
- lst.sort()
- f = open(sys.argv[2],"w",encoding="gbk")
- for x in lst:
- f.write("%s\t%d\n" % (x[0],x[1]))
- f.close()
复制代码
以下是b代码
- import sys
- import re
- def countFile(filename,words):
- try:
- f = open(filename,"r",encoding = "gbk" )
- except Exception as e:
- print(e)
- return 0
- txt = f.read()
- f.close()
- splitChars = set([])
- for c in txt:
- if not ( c >= 'a' and c <= 'z' or c >= 'A' and c <= 'Z'):
- splitChars.add(c)
- splitStr = ""
- for c in splitChars:
- if c in {'.','?','!','"',"'",'(',')','|','*','$','\\','[',']','^','{','}'}:
- splitStr += "\" + c + "|"
- else:
- splitStr += c + "|"
- splitStr += " "
- lst = re.split(splitStr,txt)
- for x in lst:
- if x == "":
- continue
- lx = x.lower()
- if lx in words:
- words[lx] += 1
- else:
- words[lx] = 1
- return 1
- result = {}
- if countFile(sys.argv[1],result) ==0:
- exit()
- lst = list(result.items())
- lst.sort()
- f = open(sys.argv[2],"w",encoding="gbk")
- for x in lst:
- f.write("%s\t%d\n" % (x[0],x[1]))
- f.close()
复制代码
附件包含ab文件和例子txt
b.zip
(1.41 KB, 下载次数: 2)
【a.py】:
- def countFile(filename,words):
- . . . . . .
- for x in lst:
- . . . . . .
- return 1 # 区别在这一行的缩进位置
复制代码
【b.py】:
- def countFile(filename,words):
- . . . . . .
- for x in lst:
- . . . . . .
- return 1 # 区别在这一行的缩进位置
复制代码
|
|