python corrupt extra field_求救,作业完全不会写啊

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

作业是一份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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值