processTxt.py 560 B

12345678910111213141516171819
  1. #出现问题conda activate base
  2. #将文本的前19万字符留下
  3. import sys
  4. def main():
  5. input_file = sys.argv[1] # 输入文件名
  6. output_file = sys.argv[1] + "out.txt" # 输出文件名
  7. chars_to_keep = 190000 # 需要保留的字符数
  8. with open(input_file, 'r', encoding='utf-8') as f_in, open(output_file, 'w', encoding='utf-8') as f_out:
  9. count = 0
  10. for line in f_in:
  11. count += len(line)
  12. if count > chars_to_keep:
  13. break
  14. f_out.write(line)
  15. if __name__ == "__main__":
  16. main()