利用python對csv檔案進行分割

2021-08-28 15:07:00 字數 1061 閱讀 8188

有個任務,就是將乙個大的csv分割為幾個小的csv,當然是的包含表頭的。

於是,我想到了類似於,用雙指標來做。

import csv

import os

path = '/users/mac/desktop/186_3.csv'

with open(path, 'r', newline='') as file:

csvreader = csv.reader(file)

a = next(csvreader)

print(a)

i = j = 1

for row in csvreader:

print(row)

print(f'i is , j is ')

# 沒1000個就j加1, 然後就有乙個新的檔名

if i % 1000 == 0:

j += 1

print(f"csv 生成成功")

csv_path = os.path.join('/'.join(path.split('/')[:-1]), '186_3/' + str(j) + '.csv')

# print('/'.join(path.split('/')[:-1]))

print(csv_path)

# 不存在此檔案的時候,就建立

if not os.path.exists(csv_path):

with open(csv_path, 'w', newline='') as file:

csvwriter = csv.writer(file)

csvwriter.writerow(['image_url'])

csvwriter.writerow(row)

i += 1

# 存在的時候就往裡面新增

else:

with open(csv_path, 'a', newline='') as file:

csvwriter = csv.writer(file)

csvwriter.writerow(row)

i += 1

python中利用GDAL對tif檔案進行讀寫

利用gdal庫對tif影像進行讀取 示例 預設波段為 b g r nir的順序,且為四個波段 import gdal defreadtif filename dataset gdal.open filename ifdataset none print filename 檔案無法開啟 return ...

利用Python處理CSV 檔案

csv 檔案 將資料作為一系列以逗號分隔的值寫入檔案,通俗的講就是兩個逗號的資訊之間看作乙個資料。csv模組包含在python標準庫中,可用於分析csv檔案中的資料行 import csv 利用matplotlib繪圖 from matplotlib import pyplot as plt fil...

利用python對CSV檔案分組並拆分檔案

指令碼背景 提取了不同類別下的購買手機號碼,由於資料量巨大,需要對資料進行分組,得到不同類別下的手機號碼,同時自動拆分檔案,按照乙個特定類別輸出檔案。import pandas as pd read data from csv df pd.read csv c users administrator...