[英]How to copy unique individuals from a csv file into new files
我對python很陌生,所以請多多包涵。 我有一個csv文件,看起來像這樣:
。
我試圖遍歷文件,並為每個唯一的人創建一個新的csv文件並復制行。 我成功地對一只動物進行了此操作,但是在為更通用的方法創建語法時遇到了麻煩。 這是我目前擁有的:
import arcpy
import csv
from csv import DictReader
WS = arcpy.env.workspace = raw_input("Where if your workspace")
infile = raw_input("where is your file?")
outfile = raw_input("What is your outfile name?")
arcpy.env.overwriteOutput = True
with open(infile, "r") as csvFile, open(outfile, "w") as out, open("outfile2.csv", "w") as out2:
reader = csv.DictReader(csvFile)
writer = csv.writer(out)
writer.writerow(reader.fieldnames)
for row in reader:
if row["Animal"] == "1":
values = [row[field] for field in reader.fieldnames]
writer.writerow(values)
要將每個Animal
寫入自己的CSV文件,您需要為每種動物打開不同的文件。 這可以通過使用字典來存儲每個動物的文件對象和csv編寫器對象來完成。 最后,這可以用來正確關閉所有文件:
import csv
output_csvs = {} # e.g. {'1' : [file_object, csv_object]}
with open('input.csv', 'rb') as f_input:
csv_reader = csv.reader(f_input)
header = next(csv_reader)
for row in csv_reader:
animal = row[0]
if animal in output_csvs:
output_csvs[animal][1].writerow(row)
else:
f_output = open('animal_{}.csv'.format(animal), 'wb')
csv_output = csv.writer(f_output)
output_csvs[animal] = [f_output, csv_output]
csv_output.writerow(header)
csv_output.writerow(row)
for csv_file, csv_writer in output_csvs.values():
csv_file.close()
這將為您提供一組根據動物命名的輸出CSV文件,例如, animal_1.csv
另外,如果數據足夠小,可以讀入內存,則可以使用Python的itertools.groupby()
函數按動物對其進行排序並一次輸出一個塊:
from itertools import groupby
import csv
with open('input.csv', 'rb') as f_input:
csv_reader = csv.reader(f_input)
header = next(csv_reader)
for animal, group in groupby(sorted(csv_reader), lambda x: x[0]):
with open('animal_{}.csv'.format(animal), 'wb') as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow(header)
csv_output.writerows(group)
使用sorted()
可確保將相同種類的所有動物組合在一起。 如果數據中已經存在這種情況,則不需要排序。
要訪問這些文件,您可以使用glob.glob()
:
import matplotlib.pyplot as plt
import glob
for animal_filename in glob.glob('animal_*.csv'):
with open(animal_filename, 'rb') as f_input:
csv_input = csv.reader(f_input)
heading = next(csv_input)
x, y = [], []
for row in csv_input:
x.append(int(row[1]))
y.append(int(row[2]))
fig, ax = plt.subplots()
plt.title(animal_filename)
ax.scatter(x, y)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.