Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save linuxinsights/ff64b25ec20513c13083673ed7c0dc39 to your computer and use it in GitHub Desktop.
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
import csv
with open('file.csv', 'r') as csvfile:
csvreader = csv.reader(csvfile)
for row in csvreader:
print(row)
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)
import csv
with open('file.csv', 'r') as csvfile:
csvreader = csv.reader(csvfile)
row_count = sum(1 for row in csvreader)
print(row_count)
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)
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)
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))
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)
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)
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)
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)
Name Age City
John 25 New York
Jane 30 Los Angeles
Name Age City
Jane 30 Los Angeles
John 25 New York
Name Age City
John 25 New York
Jane 30 Los Angeles
John 27 New York
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