电视台调度软件输出数据清洗:删除指定时间行(时间为文本格式)
Hey there! Let's work through your two challenges one by one—since you mentioned it's been a decade since you've coded, I'll keep things straightforward and avoid unnecessary jargon.
This is a common task, and the approach depends a bit on what tool you're using (like Python with pandas, or Excel VBA). Here are two practical options:
Option 1: Python with pandas (great for large datasets)
If you're using Python, pandas is the go-to library for table manipulation. Here's a step-by-step snippet:
import pandas as pd # Load your table (replace with your file path, e.g., Excel or CSV) df = pd.read_csv("your_schedule_data.csv") # Example: Delete rows where the "节目名称" column contains "重播" # Use ~ to invert the condition (keep rows that DON'T match) df = df[~df["节目名称"].str.contains("重播")] # If you need exact matches (e.g., cell equals "临时测试节目"): # df = df[df["目标列名"] != "临时测试节目"] # Save the cleaned data df.to_csv("cleaned_schedule.csv", index=False)
Option 2: Excel VBA (if you're working directly in Excel)
If your data is in Excel, a simple VBA macro can handle this. Just open the VBA editor (Alt+F11), insert a new module, and paste this:
Sub DeleteRowsByContent() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Sheet1") ' Replace with your sheet name Dim lastRow As Long lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' Target column = A ' Loop from bottom to top to avoid skipping rows when deleting For i = lastRow To 1 Step -1 ' Check if cell contains "重播" (use = "Exact Text" for precise matches) If InStr(ws.Cells(i, "A").Value, "重播") > 0 Then ws.Rows(i).Delete End If Next i End Sub
This is trickier because the time is stored as text, but we can work around that by converting it to a numeric value (like total minutes since midnight) for easy comparison. Let's cover both Python and VBA here too:
Option 1: Python with pandas
First, we'll turn the text time into minutes, then filter out the time range you don't want (e.g., 22:00 to 5:30 next day):
import pandas as pd def time_to_minutes(time_str): # Split "HH:MM" into hours and minutes, convert to total minutes hours, minutes = map(int, time_str.split(":")) return hours * 60 + minutes # Load your data df = pd.read_csv("your_schedule_data.csv") # Add a helper column with time in total minutes df["time_in_minutes"] = df["播出时间"].apply(time_to_minutes) # Delete rows where time is 22:00 (1320 mins) or later, OR 5:30 (330 mins) or earlier df = df[~((df["time_in_minutes"] >= 1320) | (df["time_in_minutes"] <= 330))] # Remove the helper column and save df = df.drop("time_in_minutes", axis=1) df.to_csv("filtered_schedule.csv", index=False)
Option 2: Excel VBA
Similar logic—convert text time to minutes, then filter:
Sub DeleteRowsByTime() Dim ws As Worksheet Set ws = ThisWorkbook.Sheets("Sheet1") Dim lastRow As Long lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row ' Time column = B Dim i As Long Dim timeStr As String Dim totalMinutes As Integer For i = lastRow To 1 Step -1 timeStr = ws.Cells(i, "B").Value ' Split text time into hours and minutes totalMinutes = Val(Split(timeStr, ":")(0)) * 60 + Val(Split(timeStr, ":")(1)) ' Delete if time is 22:00+ or 5:30- If (totalMinutes >= 22 * 60) Or (totalMinutes <= 5 * 60 + 30) Then ws.Rows(i).Delete End If Next i End Sub
Quick note: If you haven't used pandas before, just open your command prompt/terminal and run pip install pandas to install it—it's super easy.
If you can share a bit more about the exact format of your time text or the tool you're using, I can refine this even further!
内容的提问来源于stack exchange,提问作者Tod Poirier

