473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to read a file into a parallel array?

5 Nibble
Im trying to get the data stored in a file and put that data into a parallel array.
Oct 21 '20 #1
3 3651
SioSio
272 256MB
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
ideku12
5 Nibble
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
Oct 22 '20 #3
SioSio
272 256MB
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.
Oct 23 '20 #4

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

Similar topics

5
by: deko | last post by:
I have a text file ("eighty.txt") that looks like this: 83|84|85|86 I can read the file into an array like this: $numbers= file("eighty.txt"); But how do I key the array? I'd like to use...
14
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
5
by: James | last post by:
i've a text file in the following format and i would like to read into this array text file ====== machine1 machine2 Dim sr As StreamReader = New StreamReader("machines.txt") dim line as...
2
by: somequestion | last post by:
During copying file , wanna read file Size like this string CheckFileSize(string fileName) { if( fileName == null ) return; FileInfo fi = new FileInfo(fileName); return fi.Length.ToString();...
1
by: kmuz08 | last post by:
Hello to everyone, I have to do a project in my discovering programming class based on creating parallel arrays for an order form. I am still not completely sure was a parallel array...
3
by: wesley1970 | last post by:
<?php $a = array(1,2,3,4,5); $b = array(1,2,3,4,5,6,7,8); echo array_intersect($a, $b); ?> array_intersect should return the numbers of items intersect in 2 arrays. In this case, it...
14
by: lashaw | last post by:
This program display a names. If the name you type match the one asked to type, the program tells you the phone number of the name. The problem is it only list the last name in the file. How to get...
7
abdoelmasry
by: abdoelmasry | last post by:
Hi Pros I need help to read xml to array, xml file structure is unknown, and has many elements and subelements, so it should be imported to multiple array. here is xml file as example:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.