杜永斌456 2022-09-04 16:00 采纳率: 50%
浏览 54
已结题

python追加扩增数据代码无法写入文件的问题

【不报错但却写不进文件】

  • 这段目的是使用文件追加扩增数据,数据采样范围在(mean-STDEV, mean+STDEV)。东拼西凑了代码运行后没有报错但却无法写入文件,恳求帮助我改一下代码
import csv
import random
import numpy as np
import pandas as pd
# load the total data CSV file into a list
data_file_1 = open('RSDS_AI_10.csv', 'rb') 
data_list_1 = data_file_1.readlines()[1:0]
data_file_1.close()
# augment training datasets
for record in data_list_1:
    # split the record by the ',' commas
    record = record.decode()
    type(record)
    array = record.split(',')
    all_values = record.split(',')
    arr_std1 = np.std(float(all_values[1]))   #标准差
    arr_std2 = np.std(float(all_values[2])) 
    arr_std3 = np.std(float(all_values[3])) 
    arr_std4 = np.std(float(all_values[4])) 
    arr_std5 = np.std(float(all_values[5])) 
    arr_std6 = np.std(float(all_values[6]))
    arr_std7 = np.std(float(all_values[7])) 
    arr_std8 = np.std(float(all_values[8])) 
    arr_std9 = np.std(float(all_values[9])) 
    arr_std10 = np.std(float(all_values[10])) 
    arr_std11 = np.std(float(all_values[11]))
    arr_std12 = np.std(float(all_values[12]))  
    arr_std13 = np.std(float(all_values[13])) 
    arr_std14 = np.std(float(all_values[14])) 
    arr_std15 = np.std(float(all_values[15])) 
    arr_std16 = np.std(float(all_values[16])) 
    arr_std17 = np.std(float(all_values[17]))
    arr_std18 = np.std(float(all_values[18])) 
    arr_std19 = np.std(float(all_values[19])) 
    arr_std20 = np.std(float(all_values[20])) 
    arr_std21 = np.std(float(all_values[21])) 
    arr_std22 = np.std(float(all_values[22]))
    arr_std23 = np.std(float(all_values[23]))
    arr_std24 = np.std(float(all_values[24]))  
    arr_std25 = np.std(float(all_values[25])) 
    arr_std26 = np.std(float(all_values[26])) 
    arr_std27 = np.std(float(all_values[27])) 
    arr_std28 = np.std(float(all_values[28])) 
    arr_std29 = np.std(float(all_values[29]))
    arr_std30 = np.std(float(all_values[30])) 
    arr_std31 = np.std(float(all_values[31])) 
    arr_std32 = np.std(float(all_values[32])) 
    arr_std33 = np.std(float(all_values[33])) 
    arr_std34 = np.std(float(all_values[34]))
  
    arr_mean1 = np.mean(float(all_values[1]))   #平均值
    arr_mean2 = np.mean(float(all_values[2])) 
    arr_mean3 = np.mean(float(all_values[3])) 
    arr_mean4 = np.mean(float(all_values[4])) 
    arr_mean5 = np.mean(float(all_values[5])) 
    arr_mean6 = np.mean(float(all_values[6]))
    arr_mean7 = np.mean(float(all_values[7])) 
    arr_mean8 = np.mean(float(all_values[8])) 
    arr_mean9 = np.mean(float(all_values[9])) 
    arr_mean10 = np.mean(float(all_values[10])) 
    arr_mean11 = np.mean(float(all_values[11]))
    arr_mean12 = np.mean(float(all_values[12]))  
    arr_mean13 = np.mean(float(all_values[13])) 
    arr_mean14 = np.mean(float(all_values[14])) 
    arr_mean15 = np.mean(float(all_values[15])) 
    arr_mean16 = np.mean(float(all_values[16])) 
    arr_mean17 = np.mean(float(all_values[17]))
    arr_mean18 = np.mean(float(all_values[18])) 
    arr_mean19 = np.mean(float(all_values[19])) 
    arr_mean20 = np.mean(float(all_values[20])) 
    arr_mean21 = np.mean(float(all_values[21])) 
    arr_mean22 = np.mean(float(all_values[22]))
    arr_mean23 = np.mean(float(all_values[23]))
    arr_mean24 = np.mean(float(all_values[24]))  
    arr_mean25 = np.mean(float(all_values[25])) 
    arr_mean26 = np.mean(float(all_values[26])) 
    arr_mean27 = np.mean(float(all_values[27])) 
    arr_mean28 = np.mean(float(all_values[28])) 
    arr_mean29 = np.mean(float(all_values[29]))
    arr_mean30 = np.mean(float(all_values[30])) 
    arr_mean31 = np.mean(float(all_values[31])) 
    arr_mean32 = np.mean(float(all_values[32])) 
    arr_mean33 = np.mean(float(all_values[33])) 
    arr_mean34 = np.mean(float(all_values[34]))
    # build new CSV file to save new data
    p = open('D:fansile.csv', 'ab', encoding='utf-8', newline='')
    csv_writer = csv.writer(p)
    for i in range(10): # generate 10 times training data
        input1 = np.random.uniform(arr_mean1 - arr_std1, arr_mean1 + arr_std1) # randomly sampling from (mean-STDEV, mean+STDEV)
        input2 = np.random.uniform(arr_mean2 - arr_std2, arr_mean2 + arr_std2)
        input3 = np.random.uniform(arr_mean3 - arr_std3, arr_mean3 + arr_std3)
        input4 = np.random.uniform(arr_mean4 - arr_std4, arr_mean4 + arr_std4)
        input5 = np.random.uniform(arr_mean5 - arr_std5, arr_mean5 + arr_std5)
        input6 = np.random.uniform(arr_mean6 - arr_std6, arr_mean6 + arr_std6)
        input7 = np.random.uniform(arr_mean7 - arr_std7, arr_mean7 + arr_std7)
        input8 = np.random.uniform(arr_mean8 - arr_std8, arr_mean8 + arr_std8)
        input9 = np.random.uniform(arr_mean9 - arr_std9, arr_mean9 + arr_std9)
        input10 = np.random.uniform(arr_mean10 - arr_std10, arr_mean10 + arr_std10)
        input11 = np.random.uniform(arr_mean11 - arr_std11, arr_mean11 + arr_std11)
        input12 = np.random.uniform(arr_mean12 - arr_std12, arr_mean12 + arr_std12)
        input13 = np.random.uniform(arr_mean13 - arr_std13, arr_mean13 + arr_std13)
        input14 = np.random.uniform(arr_mean14 - arr_std14, arr_mean14 + arr_std14)
        input15 = np.random.uniform(arr_mean15 - arr_std15, arr_mean15 + arr_std15)
        input16 = np.random.uniform(arr_mean16 - arr_std16, arr_mean16 + arr_std16)
        input17 = np.random.uniform(arr_mean17 - arr_std17, arr_mean17 + arr_std17)
        input18 = np.random.uniform(arr_mean18 - arr_std18, arr_mean18 + arr_std18)
        input19 = np.random.uniform(arr_mean19 - arr_std19, arr_mean19 + arr_std19)
        input20 = np.random.uniform(arr_mean20 - arr_std20, arr_mean20 + arr_std20)
        input21 = np.random.uniform(arr_mean21 - arr_std21, arr_mean21 + arr_std21)
        input22 = np.random.uniform(arr_mean22 - arr_std22, arr_mean22 + arr_std22)
        input23 = np.random.uniform(arr_mean23 - arr_std23, arr_mean23 + arr_std23)
        input24 = np.random.uniform(arr_mean24 - arr_std24, arr_mean24 + arr_std24)
        input25 = np.random.uniform(arr_mean25 - arr_std25, arr_mean25 + arr_std25)
        input26 = np.random.uniform(arr_mean26 - arr_std26, arr_mean26 + arr_std26)
        input27 = np.random.uniform(arr_mean27 - arr_std27, arr_mean27 + arr_std27)
        input28 = np.random.uniform(arr_mean28 - arr_std28, arr_mean28 + arr_std28)
        input29 = np.random.uniform(arr_mean29 - arr_std29, arr_mean29 + arr_std29)
        input30 = np.random.uniform(arr_mean30 - arr_std30, arr_mean30 + arr_std30)
        input31 = np.random.uniform(arr_mean31 - arr_std31, arr_mean31 + arr_std31)
        input32 = np.random.uniform(arr_mean32 - arr_std32, arr_mean32 + arr_std32)
        input33 = np.random.uniform(arr_mean33 - arr_std33, arr_mean33 + arr_std33)
        input34 = np.random.uniform(arr_mean34 - arr_std34, arr_mean34 + arr_std34)
        csv_writer.writerow([input1, input2, input3, input4, input5, input6, input7, input8, input9, input10,input11,input12,
                             input13,input14,input15,input16,input17,input18,input19,input20,input21,input22,input23,input24
                             ,input25,input26,input27,input28,input29,input30,input31,input32,input33,input34]) # decided by your structure of dataset
    p.close()
  • 无报错但写不进去
  • 问了一个人他说我是把一个文件用两个权限不同的指针去指,每个指针有着不同的操作,不能这样写,需要借助临时文件,但具体怎么做没有说
  • 最终想要数据扩增迭代10次,并追加到csv文件中
  • 写回答

3条回答 默认 最新

  • 梦里逆天 2022-09-04 16:58
    关注

    data_file_1.readlines()[1:0]这是什么操作,从下标为1的位置开始直到下标为0的位置??

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月13日
  • 已采纳回答 9月5日
  • 创建了问题 9月4日

悬赏问题

  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
  • ¥15 潜在扩散模型的Unet特征提取
  • ¥15 iscsi服务无法访问,如何解决?
  • ¥15 感应式传感器制作的感应式讯响器