Home>

There are two csv files, the first is an array of 16000 rows and 4 columns and the second is an array of 1 row and 10 columns. First, I read these two files as a list. In the third column of this first file, 1000 0s are arranged and then 500 5s are arranged, which continues regularly up to 16000. When this 3rd column is 0, I want to add a column and output 0, if it is 5, I want to read from the second file and output it to the added column, but the first 5 is 500 consecutive When arranged, add the value in the 1st row and 1st column of the 2nd file, and when the 2nd 5 is arranged 500 times in a row, add the value in the 1st row and 2nd column of the 2nd file Then, I want to repeat the same thing until the end of the series of 5 and make the number to be written different each time. How should I program it?
Also, the python used is python 3.7.9.

Output the first csv file as a list
・ ・ ・
['0.001', '0', '5','-10']
['0','-0.001', '5','-10']
['0','-0.001', '5','-10']
['0','-0.001', '5','-10']
['0','-0.001', '5','-10']
['-0.001','-0.001', '5','-10']
['-0.001','-0.001', '5','-10']
['-0.001', '0', '5','-10']
['-0.001','-0.001', '5','-10']
['-0.001','-0.001', '5','-10']
['-0.001','-0.001', '5','-10']
['-0.001','-0.002', '5','-10']
['-0.001','-0.002', '5','-10']
['-0.001','-0.002', '0','-10']
['-0.001','-0.002', '0','-10']
['-0.001','-0.002', '0','-10']
['0','-0.002', '0','-10']
['0','-0.002', '0','-10']
['0','-0.002', '0','-10']
['0.001','-0.002', '0','-10']
['0.001','-0.002', '0','-10']
['0','-0.002', '0','-10']
['0','-0.001', '0','-10']
['0', '0', '0','-10']
['0', '0.001', '0','-10']
['0', '0.002', '0','-10']
['0', '0.003', '0','-10']
['0', '0.003', '0','-10']
・ ・ ・
Output the second csv file as a list
['7.000000000', '8.000000000', '1.000000000', '1.000000000', '4.000000000', '5.000000000', '9.000000000', '5.000000000', '1.000000000', '3.000000000']
Error message
Corresponding source code
import glob
glob.glob ("/ data/*")
glob.glob ("/ data/* 20201124 EEG/*")
glob.glob ("/ data/* 20201124 Numeric array/*")
import csv
import numpy as np
with open ('/ data \\ 20201124 Number array \\ OpenBCI_2020_11_25_16_06_55.csv','r') as csvfile:
    count_file = csv.reader (csvfile)
    for row1 in count_file:
        print (row1)
with open ('/ data \\ 20201124 EEG \\ OpenBCI_2020_11_25_16_06_55.csv','r') as csvfile:
    eeg_file = csv.reader (csvfile)
    for row2 in eeg_file:
        if "0" in row2 [2]:
            row2.append ("0")
        else: else:
            row2.append (row1)
        print (row2)
What I tried

I built the above program and ran it, but when the third column was 5, I wrote all the second files and it didn't work. (Excerpt from one copy)

['-0.004', '0.001', '5','-10', ['7.000000000', '8.000000000', '1.000000000', '1.000000000', '4.000000000', '5.000000000', '9.000000000', '5.000000000 ', '1.000000000', '3.000000000']]

Also, the program that writes 0 when the 3rd column is 0 was successful.

In addition, as a result of output,
・ ・ ・
['0.001', '0', '5','-10', '7']
['0','-0.001', '5','-10', '7']
['0','-0.001', '5','-10', '7'] The first 5 lumps
['0','-0.001', '5','-10', '7']
['0','-0.001', '5','-10', '7']
     ・ ・ ・
['-0.001','-0.001', '5','-10', '8']
['-0.001','-0.002', '5','-10', '8']
['-0.001','-0.002', '5','-10', '8'] Second 5 lumps
['-0.001','-0.002', '5','-10', '8']
['-0.001','-0.002', '5','-10', '8']
['-0.001','-0.002', '5','-10', '8']
.. .. ..
・ ・ ・
Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    What you add in row2.append (row1) is not row1 itself, but the nth number of row1, which depends on the number 5 from the top, right?

    If so, decide the value to be added according to "what number 5 mass you are in now",
    Increase the value at "the timing when the mass changes from 0 to 5".

    If this is correct, I think it would be good to do something like this around the last loop.

    number_index = 0 # What number 5 is in the mass now?
    process_5 = False # 5 is being processed?
    with open ('/ data \\ 20201124 EEG \\ OpenBCI_2020_11_25_16_06_55.csv','r') as csvfile:
        eeg_file = csv.reader (csvfile)
        for row2 in eeg_file:
            if "0" in row2 [2]:
                process_5 = False
                row2.append ("0")
            else: else:
                if not process_5:
                    number_index + = 1
                    process_5 = True
                row2.append (row1 [number_index])
            print (row2)

    I think you should think about it yourself, but I fixed the case where the beginning of row2 is not used.

    number_index = 0 # What number 5 is in the mass now?
    process_5 = False # 5 is being processed?
    with open ('/ data \\ 20201124 EEG \\ OpenBCI_2020_11_25_16_06_55.csv','r') as csvfile:
        eeg_file = csv.reader (csvfile)
        for row2 in eeg_file:
            if "0" in row2 [2]:
                if process_5:
                    process_5 = False
                    number_index + = 1
                row2.append ("0")
            else: else:
                process_5 = True
                row2.append (row1 [number_index])
            print (row2)


    The timing of index increment is changed to the place where the 5 block is exited and the 0 block is entered.

  • Answer # 2

    I think that if you use the extend method, it will be as expected.

          if "0" in row2 [2]:
                row2.append ("0")
            else: else:
                row2.extend (row1)

  • Answer # 3

    I will post a program that I thought of myself.

    i = 0
    j = 0
    with open ('/ data \\ 20201124 EEG \\ OpenBCI_2020_11_27_10_42_34.csv','r') as csvfile:
        eeg_file = csv.reader (csvfile)
        for row2 in eeg_file:
            if "0.000" in row2 [2]:
                if j! = 0:
                    j = 0
                    i + = 1
                row2.append ("0.000")
            else: else:
                row2.append (row1 [i])
                j + = 1
            print (row2)


    Even with this, I was able to output well.
    The people who answered were really helpful.
    Thank you.