search

Home  >  Q&A  >  body text

python - 运行成功,但是没相应的文件生成

       def save_file(boy,girl,count):
            file_name_boy = 'boy' + str(count) + '.txt'
            file_name_girl = 'girl' + str(count) + '.txt'
        
            boy_file = open(file_name_boy, 'w')
            girl_file = open(file_name_girl, 'w')
        
            boy_file.writelines(boy)
            girl_file.writelines(girl)
            boy_file.close()
            girl_file.close()            #把两人的对话分别放到命名不同的文件里
        def split_file(file_name):
            f = open('E:/test/dialogue.txt')
            boy=[]
            girl=[]
            count=1
            for each_line in f:
                if each_line[:6] != '======':
                    (role,line_spoken) = each_line.split(':', 1)   #每行按照:分割成1+1个子字符串,分别赋值给=前面的对象
                    if role == '小甲鱼':
                        boy.append(line_spoken)
                    if role == '小客服':
                        girl.append(line_spoken)
                else:
                    save_file(boy,girl,count)
                    boy = []
                    girl = []
                    count += 1
            save_file(boy,girl,count)
            f.close()
        split_file('E:/test/dialogue.txt')
E:\Python\python.exe "E:/PyCharm 2016.3.2/testest/abc/filelearn_01.py"

Process finished with exit code 0

http://edu.csdn.net/course/de... 是这个视频里的

大家讲道理大家讲道理2802 days ago869

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-04-18 10:20:13

    Has been successful in searching for keywords found in the work environment


    In addition, 'e:test/dialogue.txt' When the former is //, the latter can be //or or/or; when the former is /, the latter can only be single-slope.


    If you are interested, you can try it and tell me why

    reply
    0
  • PHPz

    PHPz2017-04-18 10:20:13

    The error message is not posted well. It seems that the error is caused by this code (role,line_spoken) = each_line.split(':', 1)
    When the each_line variable does not contain :, an error will occur

    reply
    0
  • 阿神

    阿神2017-04-18 10:20:13

    Encoding issues will have a greater impact in python2, so pay special attention;

    1. The default encoding when saving in Notepad under Windows is GBK, while python2 processes it according to unicode, so when opening the file, it is recommended to convert the encoding to unicode first to avoid more problems;

    2. In addition, the newline character in Notepad is "n", and Python will also read the newline character when reading the file. This may also cause unnecessary trouble for your subsequent processing. It is recommended to remove the newline character;

    3. Then there is the problem of colon. The reason for the error is that the split method cannot find the colon given by your code in the string each_line. The colon you gave is the colon in English state, while the colon in the file is the colon in Chinese state. But if you directly change it to the Chinese colon, you also need to unify the Chinese colon into a unicode string;

    4. The next step is the Chinese comparison case of role == 'Little Turtle'. You still need to tell python that you are using unicode strings, so that you can make a relatively accurate comparison.

    def split_file(file_name):
        f = open('E:/test/dialogue.txt')
        boy=[]
        girl=[]
        count=1
        for each_line in f:
            each_line = each_line.strip().decode('gbk') # strip()方法用来去掉"\n";decode()方法用来把编码从gbk解码到unicode
            if each_line[:6] != '======':
                # (role,line_spoken) = each_line.split(':', 1)   #每行按照:分割成1+1个子字符串,分别赋值给=前面的对象
                (role,line_spoken) = each_line.split(u':', 1)   #注意这里的冒号是中文的冒号,u是告诉python这是个unicode的字符串
                # if role == '小甲鱼':
                if role == u'小甲鱼':
                    boy.append(line_spoken)
                # if role == '小客服':
                if role == u'小客服':
                    girl.append(line_spoken)
            else:
                save_file(boy,girl,count)
                boy = []
                girl = []
                count += 1
        save_file(boy,girl,count)
        f.close()

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:20:13

    I think it’s a question of full-width or half-width.
    But your method is not suitable for fault tolerance.
    My suggestion is:

    split_result = each_line.split(':', 1)
    if len(split_result) < 2:
        raise RuntimeError()
    (role,line_spoken) = (split_result[0], split_result[1])

    reply
    0
  • Cancelreply