美文网首页
读取 csv 文件的两种方式:pandas, csv

读取 csv 文件的两种方式:pandas, csv

作者: 谢小帅 | 来源:发表于2019-01-10 21:20 被阅读16次
    1. pandas
    import pandas as pd
    
    ann = pd.read_csv('csv/sun37.csv')
    
    for i, row in ann.iterrows():
        print(row)
    

    每一行的数据 都带有了表头属性名

    name    wall
    r        148
    g         65
    b        137
    Name: 0, dtype: object
    
    name    floor
    r         255
    g         116
    b          69
    Name: 1, dtype: object
    

    可以这样用

    label_name = row['name']
    r = row['r']
    g = row['g']
    b = row['b']
    label[label_name] = [int(r), int(g), int(b)]
    
    1. csv
    import csv
    
    with open('csv/ade150.csv', 'r') as csv_file:
        reader = csv.reader(csv_file)
        for i, row in enumerate(reader):
            if i > 0:  # 跳过第一行
                print(row)  # list
    
    ['wall', '148', '65', '137']
    ['floor', '255', '116', '69']
    

    相关文章

      网友评论

          本文标题:读取 csv 文件的两种方式:pandas, csv

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