美文网首页
[转载]Python csv文件 相关操作

[转载]Python csv文件 相关操作

作者: AeroZ | 来源:发表于2020-08-26 15:14 被阅读0次

    https://www.cnblogs.com/liangxiyang/p/11272155.html

    import csv
    with open('/f.CSV', 'r') as f:
        reader = csv.reader(f)
        print(type(reader))
        '''获取每一行'''
        # for row in reader:
        #     print(row)
        '''获取每一行'''
        # result = list(reader)
        # print(result[0])
        '''获取某一列'''
        for i in reader:
            print(i[0])
    

    Python对CSV文件的处理
    https://cloud.tencent.com/developer/article/1357970

    Python处理CSV与List的转换

    作者:新手村的0级玩家
    链接:https://www.jianshu.com/p/61b45b6b36aa
    1.读取CSV文件到List

    def readCSV2List(filePath):
        try:
            file=open(filePath,'r',encoding="gbk")# 读取以utf-8
            context = file.read() # 读取成str
            list_result=context.split("\n")#  以回车符\n分割成单独的行
            #每一行的各个元素是以【,】分割的,因此可以
            length=len(list_result)
            for i in range(length):
                list_result[i]=list_result[i].split(",")
            return list_result
        except Exception :
            print("文件读取转换失败,请检查文件路径及文件编码是否正确")
        finally:
            file.close();# 操作完成一定要关闭
    

    2.将List写入到CSV文件中

    def writeList2CSV(myList,filePath):
        try:
            file=open(filePath,'w')
            for items in myList:
                for item in items:
                    file.write(item)
                    file.write(",")
                file.write("\n") 
        except Exception :
            print("数据写入失败,请检查文件路径及文件编码是否正确")
        finally:
            file.close();# 操作完成一定要关闭
    

    另一篇文章:使用python读取csv并转化成list of list的方法
    https://blog.csdn.net/birdflyinhigh/article/details/79685019

    利用python转置csv文件
    https://blog.csdn.net/baidu_41217766/article/details/96271603

        df = pd.read_csv(file)
        df.values
        data = df.as_matrix()
        data = list(map(list,zip(*data)))
        data = pd.DataFrame(data)
        data.to_csv('dataset/'+file,header=0,index=0)
    

    Python之str型转成int型

    https://www.cnblogs.com/fallenmoon/p/7200947.html

    def fn(x,y):
        return x*10+y
    
    def char2num(s):
        return {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}[s]  # 特别注意这里,后面还有个 [s]
    
    print(type(reduce(fn, map(char2num, '1738785'))))
    print(reduce(fn, map(char2num, '1738785')))
    
    from functools import reduce
    
    def str2int(s):
        def fn(x,y):
            return x*10+y
        def char2num(s):
            return {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}[s]
        return reduce(fn,map(char2num,s))
    
    print(str2int('365779'))
    print(type(str2int('365779')))
    
    '''str转int'''
    from functools import reduce
    def str2int(s):
        return reduce(lambda x,y:x*10+y, map(lambda s:{'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9,}[s], s))
    

    python 一行语句实现if else
    https://blog.csdn.net/weixin_44255799/article/details/100945329

    value_when_true if condition_is_ture else value_when_condition_is_false
    

    python利用numpy模块读取csv文件

    https://blog.csdn.net/u012413551/article/details/87890989

    python 借助numpy保存数据为csv格式的实现方法

    https://www.jb51.net/article/143139.htm

    借助numpy可以把数组或者矩阵保存为csv文件,也可以吧csv文件整体读取为一个数组或矩阵。

    1. csv ==> matrix

    import numpy
    my_matrix = numpy.loadtxt(open("D:\\test.csv","rb"), delimiter=",", skiprows=0)
    

    2. matrix ==> csv

    import numpy
    numpy.savetxt("new.csv", my_matrix, delimiter=',')
    

    相关文章

      网友评论

          本文标题:[转载]Python csv文件 相关操作

          本文链接:https://www.haomeiwen.com/subject/octfsktx.html