0

I tried asking this question a couple of days ago, but did not get any answers. I tried working on it a bit more and removing alot of the code arround the "broken" function But the problem is still here.

So, i am trying to get data from more or less a non standard CSV file, i open the file, read it line by line split the line by "," and do some formating on the data.

The problem is, for some reason all the data in the list is only the last entry apended.

my code as follows:

import json
import datetime

f = open("logfile-config14.json")
jasonData = json.load(f)
f.close
colIndex = []
colNames = []
colType = []
for entry in jasonData['values']:
    colIndex.append(entry['Tag'])
    nameString = entry['Display name']
    nameString = nameString.lower()
    nameString = nameString.replace(" ", "_")
    colNames.append(nameString)
    colType.append(entry['Type'])
columns = []
columns.append(colIndex)
columns.append(colNames)
columns.append(colType)
logFiles = ("C:\LogFIleCutter\logfile.0",
         "C:\LogFIleCutter\logfile.1",
         "C:\LogFIleCutter\logfile.2",
         "C:\LogFIleCutter\logfile.3",
         "C:\LogFIleCutter\logfile.4",
         "C:\LogFIleCutter\logfile.5",
         "C:\LogFIleCutter\logfile.6")

dataList = []
rowchange = []
row = []
for i in range(0, len(colIndex)):
    row.append(0)
    rowchange.append(0)
for x in range(0, 1): #len(logFiles)
    print("fetching data from: ")
    print(logFiles[x])
    logFile = open(logFiles[x] , "r")
    for x in range(0,4):
        line = logFile.readline()
        if not line:
            break
        else:
            line = line.split(",")
            TimeStamp = line[1]
            rest = line[2:]
            
            
            #TimeDate formatting
            whiteSpaces = len(TimeStamp)- len(TimeStamp.lstrip())
            TimeStamp = TimeStamp[whiteSpaces:]
            TimeStampFormat = "%a %b %d %H:%M:%S %Y"
            DT = datetime.datetime.strptime(TimeStamp, TimeStampFormat)
            #print(type(DT))
            #print(DT)
            row[0] = DT
            rowchange[0] +=1
            
            for i in range(0, len(rest)):
                rest[i] = rest[i].replace(" ","")
                if(i%2 == 0):
                    try:
                        index = colIndex.index(rest[i])
                        rowchange[index] += 1
                        if(colType[index] == "Bool"):
                            if(rest[i+1] == "0"):
                                row[index] = False
                                #print(type(row[index]))
                            else:
                                row[index] = True
                                #print(type(row[index]))
                        elif(colType[index] == "Short"):
                            row[index] = int(rest[i+1])
                            #print(type(row[index]))
                        else:
                            row[index] = rest[i+1]
                        
                    except:
                        continue
        print(x)
        print(row[0])
        dataList.append(row)
        print(dataList[x][0])
    logFile.close()
    print("file closed")
for x in range (0,len(dataList)):
    print(dataList[x][0])

The printout from the code:

fetching data from: 
C:\LogFIleCutter\logfile.0
0
2022-01-24 04:57:39
2022-01-24 04:57:39
1
2022-01-24 04:57:39
2022-01-24 04:57:39
2
2022-01-24 04:57:39
2022-01-24 04:57:39
3
2022-01-23 04:57:40
2022-01-23 04:57:40
file closed
2022-01-23 04:57:40
2022-01-23 04:57:40
2022-01-23 04:57:40
2022-01-23 04:57:40

And the lines from the logfile:

$1,      Mon Jan 24 04:57:39 2022,$2,10,$3,42,$4, 0,$5, 1,    #0, 42,   #1, 130,   #2, 144,    #3, 49,    #4, 45,    #5, 66,  #6, 2975, #7, -3981,  #8, 4028,  #9, 3900, #10, 2480, #13, 4029, #16, 2930, #17, 2190,  #20, 102,    #21, 2, #24, 2900, #25, 2200,    #26, 6,   #27, 51, #30, 2898,    #31, 0,    #32, 0,  #33, 150,  #34, 511,   #35, -2,   #36, 22,  #37, 549,   #38, -2,   #39, 22,   #40, 60,   #41, 45,  #42, 218, #43, -306,  #45, 236, #46, -152,  #48, 100,    #49, 0,  #50, 100,    #51, 0,  #52, 137, #53, -200,  #55, 137,  #56, -64, #58, 7850,    #59, 1, #60, 8300,    #61, 1,  #62, 100,   #63, -1,   #64, 60,   #65, 88,   #66, 86,  #67, 108,    #68, 1, #73, 1800, #74, 2500,    #76, 0,    #77, 0,    #78, 0,    #79, 0,    #80, 0,  #81, 173,  #82, 174,    #83, 0,    #84, 0,  #85, -11, #86, -100,  #88, 400, #89, 2729,    #90, 0, #91, 2762,    #93, 5,   #94, 15,   #95, 11,   #96, 99,   #97, 30,    #98, 0,   #100, 0,   #101, 0,   #102, 0,#105, 1252,   #106, 0,   #107, 0,   #108, 0,#109, 4029,   #110, 0,#111, 3900,#112, 2480,#113, 2520,#114, 2200,#115, 2900,   #116, 1,   #117, 0,   #118, 0, #119, 156, #120, 165,   #121, 0,   #123, 0,   #124, 0,  #126, 40,  #127, 60, #128, 350, #129, 450,  #130, 57,#131, 1740,#132, 1682,   #133, 1,   #134, 0,   #135, 0,   #136, 0,   #137, 2,   #138, 0, #141, 135,   #142, 0,#150, 4800,   #151, 0,   #152, 0,#153, 4864,   #154, 1,   #155, 1,   #156, 0,   #157, 0,   #158, 1,  #160, 15,  #161, 40,  #162, 15,  #163, 40,   #164, 1,   #165, 1,   #166, 0,#170, 1000,#172, 3000,   #173, 0,   #175, 0,   #179, 0, #180, 599,   #181, 0,   #182, 9,  #183, 42,   #184, 0,   #185, 7,  #186, 49,   #187, 0,   #188, 4,   #189, 1,   #190, 1,#191, 2261,#192, 1796,   #193, 1,   #194, 0,   #197, 0,#199, 2000,#200, 10000,  #201, 10,  #202, 10,#203, 3000,   #204, 0, #205, 900,   #206, 0,#209, 1774, #210, 300, #211, 900,#212, 1800, #213, 100,#217, 3000,#218, 4500,#219, 1500,#220, 3000,  #222, 60,   #223, 9,#224, 3150,   #225, 0,   #226, 0,  #231, 30,   #232, 1,   #233, 0,   #234, 0, #235, 100,   #236, 0,   #237, 0,  #240, 34,  #241, 29,   #251, 0,
$1,      Mon Jan 24 04:57:39 2022,$2,10,$3,42,$4, 0,$5, 1,  #8, 4029, #30, 2897,  #82, 173, #141, 132,#153, 5120,   #185, 8,
$1,      Mon Jan 24 04:57:39 2022,$2,10,$3,42,$4, 0,$5, 1, #30, 2898, #91, 2761,  #130, 17,#131, 1701,#132, 1683, #141, 130,#153, 5376,   #155, 0,   #185, 9,
$1,      Mon Jan 23 04:57:40 2022,$2,10,$3,42,$4, 0,$5, 1, #7, -3982,  #8, 4028, #89, 2731, #141, 128,#153, 5632,   #185, 8,

So as it can be seen when i read the data it's correct but when i go through the list it's not correct.

  • You're pushing references to `row` into `dataList` and they all point to the same thing. So all the data in `dataList` is a copy of the last values in `row` – Nick Mar 29 '22 at 07:08
  • Did you try debugging using breakpoints and stepping through your code as it executes? – Tarik Mar 29 '22 at 07:09
  • Does this answer your question? [Why does foo.append(bar) affect all elements in a list of lists?](https://stackoverflow.com/questions/6360286/why-does-foo-appendbar-affect-all-elements-in-a-list-of-lists) – Nick Mar 29 '22 at 07:22
  • Yes Thx alot Nick – Sebastian Klindt Mar 29 '22 at 09:10

0 Answers0