Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
05-25-2024, 11:12 AM
|
#16
|
Moderator
Registered: Aug 2002
Posts: 26,112
|
Code:
#!/usr/bin/python3
import openpyxl
from array import *
wb = openpyxl.load_workbook("test.xlsx")
sheet = wb.active
# find where the data starts and delete the random text
for i, row in enumerate(sheet):
name = row[0].value
if name == "Action":
sheet.delete_rows(1, i)
break
col1=[]
col2=[]
for column in sheet.iter_cols():
column_name = column[0].value
if column_name == "Dpt":
for i, cell in enumerate(column):
col1.append(cell.value)
if column_name == "Total":
for i, cell in enumerate(column):
col2.append(cell.value)
print(col1)
print(col2)
I was trying to come up with something more clever but I failed. This collects the data in two arrays and I will leave it up to you to do something with it.
|
|
|
05-26-2024, 03:31 AM
|
#17
|
Senior Member
Registered: Mar 2020
Posts: 3,706
Rep:
|
I'd try DataFrame.transpose() (or just DataFrame.T) then grepping for the lines that contain required data then transpose again.
An equivalent of grep in pandas could be achieved via list comprehension:
Code:
df.loc[ : , [(df[col].str.contains('Total|Team', case=False)).any() for col in df.columns]]
OTOH, you probably can directly do something like this on rows as well (i.e. df.index rather then df.columns) without transposition.
Last edited by shruggy; 05-26-2024 at 04:06 AM.
|
|
|
05-29-2024, 03:46 PM
|
#18
|
Member
Registered: Oct 2018
Distribution: Ubuntu 24.04
Posts: 50
Original Poster
Rep:
|
I came up with the following after some hours reading bash tutorials:
Code:
#!/bin/env bash
# One-liner (without mkdir and file rename)
# for FILE in *.csv; do for i in $(seq $(csvcut -n "$FILE" | wc -l)); do csvcut -H -c $i "$FILE" > columns/$i; done && grep -H -m 1 -i "Total\|Team" columns/* | awk 'length < 50' | cut -d "/" -f 2 | cut -d ":" -f 1 | while read line; do csvcut -c $line "$FILE" > ./sheets/$line; done && for n in $(seq 9); do if [ -f $i ]; then mv $i 0$i; fi; done && paste sheets/* > "$FILE.temp"; done
mkdir -p columns
mkdir -p sheets
mkdir -p newcsv
# For each csv file in current directory
for FILE in *.csv; do
# Find number of columns in current file using csvcut and wc
for i in $(seq $(csvcut -n "$FILE" | wc -l)); do
# Separate all columns into files into the column directory
csvcut -H -c $i "$FILE" > columns/$i
done
## Should probably use variables and separate the next line of code
# a. For each column, search for terms 'Total' and 'Team', but exclude results > 50 characters as likely irrelevant comments
# b. Cut the output to make a list of column numbers
# c. For each line of the list, use csvcut to write matching columns to sheets directory
grep -H -m 1 -i "Total\|Team" columns/* | awk 'length < 50' | cut -d "/" -f 2 | cut -d ":" -f 1 | while read line; do csvcut -c $line "$FILE" > ./sheets/$line; done
# Prepend a zero to files named as integers <= 9; e.g. 3 -> 03; so that paste joins columns in order
# (Assume there must be a better way than having to do this)
for n in $(seq 9); do if [ -f $n ]; then mv $n 0$n; fi; done
# Join columns together
paste sheets/* > "$FILE.temp"
done
# Rename files and move to new directory
for file in *.csv.temp; do
mv -- "$file" "${file%.csv.temp}_filtered.csv"
done
mv *_filtered.csv newcsv
Quote:
Originally Posted by michaelk
Code:
#!/usr/bin/python3
import openpyxl
from array import *
wb = openpyxl.load_workbook("test.xlsx")
sheet = wb.active
# find where the data starts and delete the random text
for i, row in enumerate(sheet):
name = row[0].value
if name == "Action":
sheet.delete_rows(1, i)
break
col1=[]
col2=[]
for column in sheet.iter_cols():
column_name = column[0].value
if column_name == "Dpt":
for i, cell in enumerate(column):
col1.append(cell.value)
if column_name == "Total":
for i, cell in enumerate(column):
col2.append(cell.value)
print(col1)
print(col2)
I was trying to come up with something more clever but I failed. This collects the data in two arrays and I will leave it up to you to do something with it.
|
Thanks. I will study this tomorrow.
Last edited by lxs602; 05-31-2024 at 09:03 AM.
|
|
|
All times are GMT -5. The time now is 02:21 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|