test6sample.txtからランダムに4組の文章をとってきて
1つ目の文にあり、かつ2つ目の文にも存在するフレーズを出力し次のように表現したいコードを書きたいのですが、59行目でたまにエラーを吐きます。str1[word_line+b+1]が存在しませんと出るのですが58行目でエラーを回避してるつもりができていません。
やりたいこと
2つの文章がこうだとする
1つ目
abcxxxacxxx
2つ目
zzzabczzzzz
理想の出力
[[abc,xxx,a,c,xxx][1,0,1,1,0]]
こんな感じに出力したいのですが、文字列を取得したときに改行も含んでしまうため、コードを書くのにすごく複雑になってしまいました。
python
1 2# coding: utf-8 3import random 4 5 6for x in range(4): 7 line_num = len(open("test6sample.txt").readlines()) 8 num_1 = 0 9 num_2 = 0 10 while num_1 == num_2: 11 num_1 = random.randint(0,line_num - 1) 12 num_2 = random.randint(0,line_num - 1) 13 #num_1 = 7 14 #num_2 = 8 15 f = open("test6sample.txt", "r") 16 str1 = f.readlines()[num_1] 17 f.close() 18 f = open("test6sample.txt", "r") 19 str2 = f.readlines()[num_2] 20 f.close() 21 22 #単語検出プログラム 23 word_lines = 0 24 word_len = 0 25 word_else = [] 26 print(len(str1), str1) 27 print(len(str2), str2) 28 open("test6-2.txt","a").write("[[") 29 while word_lines < len(str1): 30 word_len = 0 31 #print("point1","wl=",word_lines,"wn=",word_len,str1[word_lines]) 32 for b in range(len(str1)-word_lines): 33 word_len += 1 34 if str1[word_lines:word_lines+b] in str2 and str1[word_lines:word_lines+b] != "": 35 #print("point2","wl=",word_lines,"wn=",word_len,str1[word_lines]) 36 if str1[word_lines:word_lines+b] in str2 and not(str1[word_lines:word_lines+b+1] in str2): 37 #print("point3","wl=",word_lines,"wn=",word_len,str1[word_lines]) 38 open("test6.txt","a").write(str1[word_lines:word_lines+b] + "\n") 39 if str1[word_lines+b] != "\n": 40 #print("point4","wl=",word_lines,"wn=",word_len,str1[word_lines]) 41 open("test6-2.txt","a").write(str1[word_lines:word_lines+b]+",") 42 word_else.append(1) 43 else: 44 #print("point5","wl=",word_lines,"wn=",word_len,str1[word_lines]) 45 open("test6-2.txt","a").write(str1[word_lines:word_lines+b]) 46 word_else.append(1) 47 #print("出力1: ",str1[word_lines:word_lines+word_len]) 48 word_lines = word_lines + b - 1 49 elif str1[word_lines+b] == "\n": 50 open("test6-2.txt","a").write(str1[word_lines:word_lines+b]) 51 word_lines = word_lines + b - 1 52 word_else.append(1) 53 else: 54 #print("point6","wl=",word_lines,"wn=",word_len,str1[word_lines]) 55 if str1[word_lines:word_lines+b] in str2 and not(str1[word_lines:word_lines+b+1] in str2) and str1[word_lines+b] != "\n": 56 #print("point7","wl=",word_lines,"wn=",word_len,str1[word_lines]) 57 open("test6-2.txt","a").write(str1[word_lines+b]) 58 #print("出力2: ",str1[word_lines+word_len]) 59 if str1[word_lines+b] != "\n": 60 if str1[word_lines+b+1] in str2 and str1[word_lines+b] != "\n": 61 #print("point8","wl=",word_lines,"wn=",word_len,str1[word_lines]) 62 if str1[word_lines+b+1] != "\n": 63 #print("point9","wl=",word_lines,"wn=",word_len,str1[word_lines]) 64 open("test6-2.txt","a").write("@") 65 word_else.append(0) 66 else: 67 #print("point0","wl=",word_lines,"wn=",word_len,str1[word_lines]) 68 word_else.append(0) 69 word_lines += 1 70 open("test6-2.txt","a").write("]") 71 open("test6-2.txt","a").write("," + str(word_else)) 72 open("test6-2.txt","a").write("]\n") 73open("test6-2.txt","a").write("\n") 74 75 76 77 78 79 80 81 82
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。