Python Forum
Import CSV data into array and turn into integers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import CSV data into array and turn into integers
#1
Hey guys,

I am working on a school project and need to make it so that i can import data from a CSV file into an array and use it to do calculations. I am able to import the file but when i do it imports as strings. I also need that data in an array. This is what i have:
import csv 

x = []
y = []

with open('FinalData.csv') as csvfile:    
	csvReader = csv.reader(csvfile)    
	for row in csvReader:        
		x.append(row[0]), y.append(row[1])
        
print(x,y)
It will print the arrays but the number values will be strings. Any idea how to change?
Thanks for the help
Reply
#2
In Python you have an option called list for the concept of array.
I couldn't able to get what your expected output will look like.
This might work !
import csv 
data =[]
 
with open('FinalData.csv') as csvfile:    
    csvReader = csv.reader(csvfile)    
    data = list (csvReader)
         
print(data) 
Reply
#3
You need to use the int of float functions to convert the strings to numbers.
Reply
#4
(Jul-16-2020, 04:21 AM)ndc85430 Wrote: import csv
data =[]
  
with open('FinalData.csv') as csvfile:   
    csvReader = csv.reader(csvfile)   
    data = list (csvReader)
          
print(data)

Can you explain? i tried adding x = int(x) but i got the error
int() argument must be a string, a bytes-like object or a number, not 'list'
Reply
#5
That quote is wrong; I didn't write that code.

In any case, the error is self explanatory: you can't pass a list to int, only individual strings. So either iterate over the list calling it in each one or use a list comprehension (or map).
Reply
#6
If floats are OK for calculations then one can use QUOTE_NONNUMERIC with csv.reader.

import csv

with open('numbers', 'r', newline='') as f:
    reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
    floats = list(*zip(*reader))
# floats value with random sample data, one value per row in 'numbers' file:
[5.0, 7.0, 12.0, 65.0, 7.0, 12.0, 65.0]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 847 May-29-2023, 02:42 PM
Last Post: Nietzsche
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,871 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Python 3.11 data import question love0715 2 811 Mar-05-2023, 06:50 PM
Last Post: snippsat
  Can't import csv data JonWayn 4 1,393 Sep-18-2022, 02:07 AM
Last Post: JonWayn
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,142 Jul-11-2022, 08:54 PM
Last Post: Larz60+
Question Change elements of array based on position of input data Cola_Reb 6 2,130 May-13-2022, 12:57 PM
Last Post: Cola_Reb
  Reading data to python: turn into list or dataframe hhchenfx 2 5,386 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  how to print all data from all data array? korenron 3 2,472 Dec-30-2020, 01:54 PM
Last Post: korenron
  I try to import data from Excel table to Word Template. NewbiePyPy 0 3,276 Oct-21-2020, 12:25 PM
Last Post: NewbiePyPy
  Turn coordinates in string into a tuple Kolterdyx 3 2,768 Jun-10-2020, 05:04 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020