avatar
fireworks99
keep hungry keep foolish

python 读写csv文件

Python读取csv文件统计数据写入新的csv文件

原始资料文件

原始资料csv文件

Code

'''
pycharm里,写完了暂时没有用到的东西会显示为灰色
就像eclipse似的,写出来暂时没用到的东西会有标注
'''
import csv

data_path = './train.csv'

def run_main():

    pclass_dict = {}
    gender_dict = {}
    age_dict = {}
    sibsp_dict = {}
    parch_dict = {}
    fare_dict = {}
    embarked_dict = {}

    with open(data_path, 'r', newline='') as csvfile:
        rows = csv.reader(csvfile)
        for i, row in enumerate(rows):
            if i == 0:
                continue

            if i % 50 == 0:
                print('正在处理第{}行数据...'.format(i))

            survived = row[1]
            pclass = row[2]
            gender = row[3]
            age = row[4]
            sibsp = row[5]
            parch = row[6]
            fare = row[7]
            embarked = row[8]


            pclass = pclass.replace(' ', '')
            gender = gender.replace(' ', '')
            age = age.replace(' ', '')
            sibsp = sibsp.replace(' ', '')
            parch = parch.replace(' ', '')
            fare = fare.replace(' ', '')
            embarked = embarked.replace(' ', '')

            if gender not in gender_dict:
                gender_dict[gender] = [0, 0]
            if survived == '0':                     # 字符0
                gender_dict[gender][0] += 1
            elif survived == '1':
                gender_dict[gender][1] += 1
            else:
                pass

            if pclass not in pclass_dict:
                pclass_dict[pclass] = [0, 0]
            if survived == '0':
                pclass_dict[pclass][0] += 1
            elif survived == '1':
                pclass_dict[pclass][1] += 1
            else:
                pass

            if age not in age_dict:
                age_dict[age] = [0, 0]
            if survived == '0':
                age_dict[age][0] += 1
            elif survived == '1':
                age_dict[age][1] += 1
            else:
                pass

            if sibsp not in sibsp_dict:
                sibsp_dict[sibsp] = [0, 0]
            if survived == '0':
                sibsp_dict[sibsp][0] += 1
            elif survived == '1':
                sibsp_dict[sibsp][1] += 1
            else:
                pass

            if parch not in parch_dict:
                parch_dict[parch] = [0, 0]
            if survived == '0':
                parch_dict[parch][0] += 1
            elif survived == '1':
                parch_dict[parch][1] += 1
            else:
                pass

            if fare not in fare_dict:
                fare_dict[fare] = [0, 0]
            if survived == '0':
                fare_dict[fare][0] += 1
            elif survived == '1':
                fare_dict[fare][1] += 1
            else:
                pass

            if embarked not in embarked_dict:
                embarked_dict[embarked] = [0, 0]
            if survived == '0':
                embarked_dict[embarked][0] += 1
            elif survived == '1':
                embarked_dict[embarked][1] += 1
            else:
                pass

    print('Finished!')

    with open('result.csv', 'w', newline='', encoding='utf-16') as csvfile:
        csvwriter = csv.writer(csvfile, delimiter=',')
        csvwriter.writerow(['gender', 'dead', 'survived'])
        # csvwriter.writerow(['male', gender_dict['male'][0], gender_dict['female'][1]])
        # csvwriter.writerow(['female', gender_dict['female'][0], gender_dict['female'][1]])
        for k, v in list(gender_dict.items()):
            csvwriter.writerow([k, v[0], v[1]])

        csvwriter.writerow([ 'pclass', 'dead', 'survived'])
        for k, v in list(pclass_dict.items()):
            csvwriter.writerow([k, v[0], v[1]])               # 小括号里有中括号

        csvwriter.writerow([ 'age', 'dead', 'survived' ])
        for k, v in list(age_dict.items()):
            csvwriter.writerow([k, v[0], v[1]])

        csvwriter.writerow(['sibsp', 'dead', 'survived'])
        for k, v in list(sibsp_dict.items()):
            csvwriter.writerow([k, v[0], v[1]])

        csvwriter.writerow([ 'parch', 'dead', 'survived'])
        for k, v in list(parch_dict.items()):
            csvwriter.writerow([k, v[0], v[1]])

        csvwriter.writerow([ 'fare', 'dead', 'survived' ])
        for k, v in list(fare_dict.items()):
            csvwriter.writerow([k, v[0], v[1]])

        csvwriter.writerow([ 'embarked', 'dead', 'survived' ])
        for k, v in list(embarked_dict.items()):
            csvwriter.writerow([k, v[0], v[1]])


if __name__ == '__main__':
        run_main()



'''
            这段代码有问题
            gender_dict['male'] = [0, 0]
            gender_dict['female'] = [0, 0]
            if gender == 'male':
                if survived == '0':
                    gender_dict['male'][0] += 1          # dead mem
                elif survived == '1':
                    gender_dict['male'][1] += 1          # lived men
                else:
                    pass
            elif gender == 'female':
                if survived == '0':
                    gender_dict['female'][0] += 1        # dead women
                elif survived == '1':
                    gender_dict['female'][1] += 1        # lived women
                else:
                    pass
            else:
                pass
'''

生成的初始文件经“导入”处理后的文件

生成的初始csv文件经导入后的csv文件

Site by Baole Zhao | Powered by Hexo | theme PreciousJoy