zoukankan      html  css  js  c++  java
  • 中英文分离

    由于没有安装 numpy 

    根据博客提示,成功安装了numpy ,http://blog.csdn.net/ybuiipl/article/details/60875304#reply。

    运行之后没有错误,可是嘛,我看不到结果。也就随它去了。

    主要有两个问题,一个是运行的时候出现的

    ValueError: need more than 0 values to unpack  ,对于空行就会报错。不机智。于是加了个判断。让它一直走下去。  

    http://bbs.fishc.com/thread-63664-1-1.html,解决方案。

    大概就两个问题,可是我还是没有实现自己想要的中英文分离的功能,我都没看到。醉了。应用在实践工作中才有用啊。shit。

    看来这个事情没我想的那么简单啊

    import numpy as np
    
    filename = r'D:FISRT11	est.txt' # txt文件和当前脚本在同一目录下,所以不用写具体路径
    pos = []
    Efield = []
    
    with open(filename, 'r') as file_to_read:
        while True:
            lines = file_to_read.readline() # 整行读取数据
            if len(lines.split()) <=25:
                break
                pass
            p_tmp, E_tmp = [float(i) for i in lines.split()] # 将整行数据分割处理,如果分割符是空格,括号里就不用传入参数,如果是逗号, 则传入‘,'字符。
    
            pos.append(p_tmp)  # 添加新读取的数据
            Efield.append(E_tmp)
            pass
        pos = np.array(pos) # 将数据从list类型转换为array类型。
        Efield = np.array(Efield)
        pass

    同事写的,只能说不错。

    path = "C:/Users/ys2566183/Desktop/test.txt"
    
    file = open(path,'r',encoding="gb2312")
    for line in file.readlines():
        if len(line)>1:    (这个什么意思)#(判断这line的长度,空行肯定是零了,不是空行所以进行下面的处理),shit
            index = line.index("Z")
            line = line[index:]
            index = line.index(" ")
            line = line[:index]
            name = line.split("__")[1]  # 取分隔完数组之后的,第二个。
            print(name)

    源文件:
    省份代码 Z0FWDDM__Z0PROVINC CHAR 006  

    自己老是编码有问题,太不舒服了。 

     很烦,同一段代码。老是这里那里出点问题。难受

    import time
    
    try:
        path = r'D:FISRT11柏喜.txt'
        outh = r'D:FISRT11yes.txt'
        files = open(path,'r')
        yes = open (outh,'w')
        for line in files.readlines():
            if len(line)>1:
                name =line.split()[1]
                name = name.split('__')[1]
                print name
                yes.write(name+'
    ')
                
    finally:
        files.close()
        print '关闭文件'
    

      因为用的是2.7, line.split(),里面不用加任何的  “”,单双引号。默认的就是空格分割。

      其次就是写入文件,先前面打开。然后重复循环的写入进去。这次才是正确的事。

      总算完成了。

  • 相关阅读:
    Java知识点:javac命令
    Java知识点:内部类
    证明:寝室分配问题是NPC问题
    Java知识点:Object类
    Java 知识点:序列化
    Python3玩转儿 机器学习(2)
    Python3玩转儿 机器学习(1)
    python re模块findall使用
    百度URL 部分参数
    python datetime模块
  • 原文地址:https://www.cnblogs.com/sakura3/p/8403695.html
Copyright © 2011-2022 走看看