By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,400 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,400 IT Pros & Developers. It's quick & easy.

How to read a file into a parallel array?

P: 5
Im trying to get the data stored in a file and put that data into a parallel array.
Oct 21 '20 #1
Share this Question
Share on Google+
3 Replies

100+
P: 200
Since the file type is not written, I will show you how to read the csv file.

1.Use Pandas
Expand|Select|Wrap|Line Numbers
  1. import pandas as pd
  2. filename = 'sample.csv'
  3. # with headder
  4. #df = pd.read_csv(filename, header=0)
  5.  
  6. # without headder
  7. df = pd.read_csv(filename, header=None, names=['2nd', '3rd'])
  8.  
  9. valuelist1 = df.index.values
  10. valuelist2 = df.loc[:,'2nd'].values
  11. valuelist3 = df.loc[:,'3rd'].values
  12. print(valuelist1)
  13. print(valuelist2)
  14. print(valuelist3)
2.Use csv
Expand|Select|Wrap|Line Numbers
  1. import csv
  2. filename = 'sample.csv'
  3. valuelist1 = []
  4. valuelist2 = []
  5. valuelist3 = []
  6. with open(filename) as f:
  7.     reader = csv.reader(f)
  8.     for row in reader:
  9.         valuelist1.append(row[0])
  10.         valuelist2.append(row[1])
  11.         valuelist3.append(row[2])
  12. print(valuelist1)
  13. print(valuelist2)
  14. print(valuelist3)
Oct 22 '20 #2

P: 5
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
4 Weeks Ago #3

100+
P: 200
pandas
Expand|Select|Wrap|Line Numbers
  1. df = pd.read_csv(filename, header=None, sep=',', names=['2nd', '3rd'])
sep = , specifies the delimiter with a comma.

csv
Expand|Select|Wrap|Line Numbers
  1. csv.reader (filename, delimiter =',')
delimiter = , specifies the delimiter with a comma.
4 Weeks Ago #4

Post your reply

Sign in to post your reply or Sign up for a free account.