Created
April 27, 2023 08:53
-
-
Save linuxinsights/ff64b25ec20513c13083673ed7c0dc39 to your computer and use it in GitHub Desktop.
10 Python CSV Processing Scripts for System Administration Tasks | https://linuxtech.in/10-python-csv-processing-scripts-for-system-administration-tasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('file.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile) | |
for row in csvreader: | |
print(row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
data = [['Name', 'Age', 'City'], ['John', '25', 'New York'], ['Jane', '30', 'Los Angeles']] | |
with open('file.csv', 'w', newline='') as csvfile: | |
csvwriter = csv.writer(csvfile) | |
csvwriter.writerows(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('file.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile) | |
row_count = sum(1 for row in csvreader) | |
print(row_count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('file.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile) | |
for row in csvreader: | |
if row[2].strip() == 'New York': | |
print(row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('sort.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile) | |
next(csvreader) | |
sorted_csv = sorted(csvreader, key=lambda row: row[1]) | |
for row in sorted_csv: | |
print(row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
input_file = 'file.csv' | |
columns_to_extract = ['Age', 'Name'] # Replace with the header names of the columns you want to extract | |
with open(input_file, 'r') as csv_file: | |
reader = csv.DictReader(csv_file) | |
headers = reader.fieldnames | |
headers_to_print = [header for header in headers if header in columns_to_extract] | |
print(','.join(headers_to_print)) | |
for row in reader: | |
row_to_print = [row[column] for column in columns_to_extract] | |
print(','.join(row_to_print)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('file.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile) | |
data = [] | |
for row in csvreader: | |
if row[2] == 'New York': | |
row[2] = 'NY' | |
data.append(row) | |
with open('updated_file.csv', 'w', newline='') as outfile: | |
csvwriter = csv.writer(outfile) | |
csvwriter.writerows(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('file.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile) | |
next(csvreader) | |
total = 0 | |
count = 0 | |
for row in csvreader: | |
total += int(row[1]) | |
count += 1 | |
average = total / count | |
print(average) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('file.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile) | |
data = [] | |
seen = set() | |
for row in csvreader: | |
if row[2] not in seen: | |
data.append(row) | |
seen.add(row[2]) | |
with open('unique_file.csv', 'w', newline='') as outfile: | |
csvwriter = csv.writer(outfile) | |
csvwriter.writerows(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
with open('sortcl.csv', 'r') as csvfile: | |
csvreader = csv.reader(csvfile) | |
header = next(csvreader) | |
index = header.index('Age') | |
sorted_data = sorted(csvreader, key=lambda row: row[index]) | |
print(header) | |
for row in sorted_data: | |
print(row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Name | Age | City | |
---|---|---|---|
John | 25 | New York | |
Jane | 30 | Los Angeles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Name | Age | City | |
---|---|---|---|
Jane | 30 | Los Angeles | |
John | 25 | New York |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Name | Age | City | |
---|---|---|---|
John | 25 | New York | |
Jane | 30 | Los Angeles | |
John | 27 | New York |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Name | Age | City | |
---|---|---|---|
John | 25 | New York | |
Jane | 30 | Los Angeles | |
John | 25 | New York |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment