Skip to content
Advertisement

Error with using length function; output will not be anything other than one

I have multiple csv files I’ve uploaded into both google colab and jupyter notebook. I can successfully print certain lines of my file. The file contains rows of strings. When I open the file it opens the number application of my MacBook. Anyways, for some reason whenever I try to print the length of ANY line in my file, python ALWAYS tells me the length is 1. All of the strings have way more than a length of 1. I thought “maybe it’s the file itself?” Nope. Ive used multiple csv files, still 1. Its not the ide, I’ve used jupyter and google colab. I can print the lengths of words like ‘HELLO’. But, nothing correctly that’s in my file. Im assuming I have something wrong with my code even though I’ve tried multiple versions. Please let me know what’s going on. This is a simple command yet for some reason it is not working.

with open('/Users/xxx/Desktop/Silkscreen/fonts/ughuuh.csv' , newline='') as f:
    reader = csv.reader(f)
    data = list(reader)
    print((len(data[1]))
    >>output: 1

Advertisement

Answer

change to this:

print(len(data[1]))

I am making some assumptions about “data” but it looks like you are getting a list of lists.

so to use random:

import random

data = [['(O)[C@@H](O)[C@H](O2)OC OC[C@@H](O)C(O)[C@H](O)C'],['(O)[C@@H](O)[C@H][C@H](O)C']]

rand = random.choice(data)
print(len(rand[0]))

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement