python - Copy Specific Rows and Columns from a .csv into .xlsx -
i extremely new @ programming gonna rough lol, however, turtle scientist, of patience , excellent advice contribute turtle conservation ^_^
so, have .csv 1 of several turtle species in column , utm coordinates in columns b (northings) , c (eastings). need take rows species (for example every row column sptu [spotted turtle fyi]) save of rows in pre-existing .csv, overwriting of old coordinates. here's i've got far, i'm using pycharm , python 3.somethin:
for wsurvey-20150630.csv in glob.glob("c:/users/vito/desktop/gis/latitudes , longitudes/wsurvey/wsurvey-20150630.csv"):
(f_path, f_name) = os.path.split(wsurvey-20150630.csv) (f_short_name, f_extension) = os.path.splitext(f_name) ws = wb.add_sheet(f_short_name) spamreader = csv.reader(open(wsurvey-20150630.csv, 'rb')) row in "wsurvey-20150630latlon.csv": if "sptu" in row: rowx, row in enumerate(spamreader): colx, value in enumerate(row): ws.write(rowx, colx, value)
wb.save("c:/users/vito/desktop/gis/utm coordinates/sptu utm coordinates.csv")
so that's bunch of code copy-pasted off of tutorials , whatnot , tried make work, er doesn't work , horribly, horribly wrong. please think of poor, tiny, helpless turtles.
the coordinates in updated .csv read qgis map. idea map automatically updated once per day new turtle coordinates; wetlands around turtle coordinates protected. work 6 species, 5 of @ risk, of 1 species endangered , 1 threatened. save turtles!
no offense, code insanely disorganized xd. python easy program in these kinds of operations.
if want open 1 file reading , or writing, recommend using (this works csv or other text files. not microsoft excel.)
keep in mind overwrite file.
import csv open("writefile.csv", "w").close() #deletes in file. backspace if don't want file contents deleted. open("readfile.csv", "r") r, open("writefile.csv", "a") w: reader = csv.reader(r, lineterminator = "\n") writer = csv.writer(w, lineterminator = "\n") row in reader: if "sptu" in row: #"sptu" has entire value of column. please comment if want see if 1 of columns contains "sptu w.writerow(row)
for excel, best install specific python module on computer first. please find python folder (for example, have python34 because using python 3.4). now, copy build path folder , open command prompt. (if know stuff, please skip, type). type in cd
(include space) , right-click + paste
path. now, type in cd scripts
. finally, pip install openpyxl
.
now, use code create excel each csv. (based off of sample code https://openpyxl.readthedocs.org/en/latest/)
from openpyxl import workbook def writetoexcel(rows, filename): wb = workbook() ws = wb.active row in rows: ws.append(row) ws.save(filename) import csv rows = [] open("readfile.csv", "r") r: reader = csv.reader(r, lineterminator = "\n") row in reader: if "sptu" in row: #"sptu" has entire value of column. please comment if want see if 1 of columns contains "sptu rows.append(row) writetoexcel(rows, "insert_name_of_file_here.xlsx")
Comments
Post a Comment