Search code examples
pythoncsvconvertersstring-length

Python - Converting CSV file - lenght of an obiect


Got little problem here.

for row in reader:
    dane_wejsciowe.append(row)

I got some *.csv file I want to convert and my converter acts weird... the function above takes all needed columns into an array and after that I am calling those columns to count the length of it:

for line in dane_wejsciowe:
    if len(line['Mnemonik']) > len_mnemonik:
        len_mnemonik += 1

After that I am printing the record print(' - Mnemonik: ' + str(len_mnemonik)) and the result is 22 BUT the longest one is BEUS_EnergyDriveAvgDist which contains _ and I suspect its not counting special letters

and my "idiotic" idea to print it into a file was

   if len(i['Mnemonik']) < len_mnemonik:
        a = len_mnemonik - len(i['Mnemonik']) + 1
    else:
        a = 1
    jsfile.write(i['Mnemonik']),
    jsfile.write((' ')*a),

Any1 have better idea to do it? :)


Solution

  • It appears you're using csv.reader() to gather each row, which automatically parses each row into a list using the specified delimiter in the command (default is comma). Because of this, your variable dane_wejsciowe is a list(?) of lists, where each outer element (e.g. dane_wejsciowe[0]) is a row of the CSV file, and each inner element (e.g. dane_wejsciowe[0][0]) is a cell in that row of the CSV. Therefore your len()==22 has nothing to do with the text BEUS_EnergyDriveAvgDist, but tells you the the number of columns in your CSV.