该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
作业是一份EXCEL,要把excel里的raw data处理成想要的样子,但是我才学了两周完全不会啊。
1. 要把excel的第一列去掉
2. raw data 2,3,4列的长度不能超过30个字母,其余的如果超过了显示corrupt
3. 第5列的全部要大写,第9列的首字母大写等等
from assign1_utilities import get_column, replace_column, truncate_string
def remove_athlete_id(row) :
i = 0
while row[i] != ',':
i += 1
# take the slice from one character after the comma
return row[i + 1:]
def is_string_valid(string, valid_characters):
# iff = if and only if
"""
(bool) Returns True iff slicing is made up only of valid characters
Paramaters:
string (str): The string to check
valid_characters (str): The only valid characters
"""
for character in string:
if character not in valid_characters:
return False # invalid string
return Ture # valid string
import string
VALID = string.ascii_letters + string.digits + "-' "
#VALID = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
# is_string_valid("dunder mifflin", VALID)
def check_place(place):
return(len(place) <= 3
and (place.isdigit() or place ==''
or place =='DNS' or place =='DNF' or place == 'PEN'))
def main() :
"""Main functionality of program."""
with open("athlete_data.csv", "r") as raw_data_file, \
open("athlete_data_clean.csv", "w") as clean_data_file :
for row in raw_data_file :
corrupt = False
row = remove_athlete_id(row)
row_to_process = row # Saves row in original state, minus athlete id.
"""place>3"""
place = get_column(row_to_process, 4)
if len(place) > 3 or not(place.isdigit() or place == ''
or place == 'DNS'
or place == 'DNF' or place == 'PEN'):
corrupt = True
# You need to implement the functionality of your program
# inside of this loop. The variable 'row' is a string containing
# a single row of data from the raw data file.
# It is advisable to implement the functionality in a number of
# functions and call those functions from inside this loop.
# Continue your processing logic here.
# Save the row data to the cleaned data file.
if not corrupt:
clean_data_file.write(row_to_process)
else :
row = row[:-1] # Remove new line character at end of row.
clean_data_file.write(row + ",CORRUPT\n")
# Call the main() function if this module is executed
if __name__ == "__main__" :
main()