读取csv文件中指定的行数据-转载

######创建csv文件,并按行读取数据#######

 

import numpy as np 

import pandas as pd

import csv

 

# 1.创建csv文件

with open('123.csv','w',newline='') as file:

    csv_writer=csv.writer(file)

# 2.读取csv指定行的数据

    with open('C:\\Users\\86178\\Desktop\\python\\pythonProject\\ethylene_methane.csv', 'r') as f:

        reader = csv.reader(f)

        index = 0

        for row in reader:

            index = index + 1

            # 3.要求

            if index %30000 ==0:

                # 4.写入满足要求的行数据

                csv_writer.writerow(row)

######添加表头#######

# 1.读取csv文件

dg = pd.read_csv('C:\\Users\\86178\\Desktop\\python\\pythonProject\\123.csv',header=None)

# 2.添加表头

dg = dg.rename(columns = {0:'Num', 1:'Time (seconds)', 2:'Methane conc (ppm)',3:'Ethylene conc(ppm)',4:'Sensor1',5:'Sensor2',6:'Sensor3',7:'Sensor4',8:'Sensor5',9:'Sensor6',10:'Sensor7',11:'Sensor8',12:'Sensor9',13:'Sensor10',14:'Sensor11',15:'Sensor12',16:'Sensor13',17:'Sensor14',18:'Sensor15',19:'Sensor16'})

# 3.保存csv文件

dg.to_csv('123.csv')

————————————————

版权声明:本文为CSDN博主「熊不胜」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/asdfghjkl9514/article/details/123595144

默认 最新
当前暂无评论,小编等你评论哦!
点赞 2 评论 收藏
关注