How to read a file into a parallel array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ideku12
    New Member
    • Oct 2020
    • 5

    How to read a file into a parallel array?

    Im trying to get the data stored in a file and put that data into a parallel array.
  • SioSio
    Contributor
    • Dec 2019
    • 272

    #2
    Since the file type is not written, I will show you how to read the csv file.

    1.Use Pandas
    Code:
    import pandas as pd
    filename = 'sample.csv'
    # with headder
    #df = pd.read_csv(filename, header=0)
    
    # without headder
    df = pd.read_csv(filename, header=None, names=['2nd', '3rd'])
    
    valuelist1 = df.index.values
    valuelist2 = df.loc[:,'2nd'].values
    valuelist3 = df.loc[:,'3rd'].values
    print(valuelist1)
    print(valuelist2)
    print(valuelist3)
    2.Use csv
    Code:
    import csv
    filename = 'sample.csv'
    valuelist1 = []
    valuelist2 = []
    valuelist3 = []
    with open(filename) as f:
    	reader = csv.reader(f)
    	for row in reader:
    		valuelist1.append(row[0])
    		valuelist2.append(row[1])
    		valuelist3.append(row[2])
    print(valuelist1)
    print(valuelist2)
    print(valuelist3)

    Comment

    • ideku12
      New Member
      • Oct 2020
      • 5

      #3
      Oh I forgot to say the file type. So im trying to read the data from a txt file into a paralell array and without using pandas. I'm sorry I should have been more clear

      Comment

      • SioSio
        Contributor
        • Dec 2019
        • 272

        #4
        pandas
        Code:
        df = pd.read_csv(filename, header=None, sep=',', names=['2nd', '3rd'])
        sep = ‘,’ specifies the delimiter with a comma.

        csv
        Code:
        csv.reader (filename, delimiter =',')
        delimiter = ‘,’ specifies the delimiter with a comma.
        Last edited by SioSio; Oct 23 '20, 01:09 AM. Reason: add. case of using pandas.

        Comment

        Working...