Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

dataframe - how can I take csv row's first item as a list name in python?

I have a csv file like this and I want to take

A1,R,N,R,U,N,N,R,U,R,R

A2,R,U,R,R,N,N,R,N,N,N

B1,R,N,R,R,R,N,N,N,R,U

Now I want to get a list like this:

A1=[R,N,R,U,N,N,R,U,R,R]

A2=[R,U,R,R,N,N,R,N,N,N]

B1=[R,N,R,R,R,N,N,N,R,U]

how can I do that?

for x in range(len(a)):
        b = a[x].split(",")     
        doc_line.append(b)


for x in range(len(doc_line)):
    print(doc_line[x])

for x in doc_line:
        engine.append(x[0])             
        rel.append(x[1]) 

  
question from:https://stackoverflow.com/questions/65939816/how-can-i-take-csv-rows-first-item-as-a-list-name-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can read row-wise from a csv file, below is the code for that.

from csv import reader

with open('sample.csv', 'r') as csv_file:
    read_csv = reader(csv_file)

    for row in read_csv:
        print(row)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...