How to list a column of float values from csv file without importing Python

0 votes

I had extracted some value from an excel file, like this:

with open("file.csv","r")  as file:

   data = file.readlines()[1:]

   for lines in data:

        lines = lines.split(",")

        A=float(lines[4])

From the above, i got a column of float when i type print(A), and there are a total of a thousand value (in a vertical column in the shell). If i want to have those values become a list, how should i do?

like turning:

0.0

0.0

0.0

into [0.0,0.0,0.0,...]

There is one restriction, not allowed to import any modules.

Sep 16, 2020 in Python by anonymous
• 120 points

edited Sep 16, 2020 by Gitika 8,938 views

1 answer to this question.

0 votes

Using the default CSV module

Demo:

import csv
with open(filename, "r") as infile:
    reader = csv.reader(infile, delimiter=' ')
    OutputList = [map(float, list(i)) for i in zip(*reader)]

print(OutputList)

Output:

[[1.0, 2.0, 3.0, 4.0, 5.0], [0.0, 0.0, 0.0, 0.0, 0.0], [5.0, 4.0, 3.0, 2.0, 1.0]]
OR

You can make use of this too as given-below:

from itertools import izip_longest
import csv
with open(filename, "r") as infile:
    reader = csv.reader(infile, delimiter=' ')
    OutputList = [map(float, [j for j in list(i) if j is not None]) for i in izip_longest(*reader)]

print(OutputList)
answered Sep 16, 2020 by Rajiv
• 8,870 points

Related Questions In Python

–1 vote
2 answers
+1 vote
0 answers

Sum the values of column matching and not matching from a .txt file and write output to a two different files using Python

Name                                                    value DR_CNDAOFSZAPZP_GPFS_VOL.0 139264 DR_CNDAOFSZAPZP_GPFS_VOL.1 15657 DR_CNDAOFSZAPZP_GPFS_VOL.0 139264 DR_CNDAOFSZAPZP_GPFS_VOL.1 156579 DR_CNDAOFSZAPZP_GPFS_VOL.2 156579 DR_CNDAOFSZAPZP_GPFS_VOL.3 ...READ MORE

Nov 20, 2019 in Python by Sagar
• 130 points
1,186 views
0 votes
1 answer

How to find the value of a row in a csv file in python and print column and row of that value ?

Hello @Khanhh , Use panda to find the value of ...READ MORE

answered Oct 15, 2020 in Python by Niroj
• 82,840 points
6,768 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
3,458 views
0 votes
1 answer

How to find the value of a row in a csv file in python?

If you want to find the value ...READ MORE

answered May 20, 2019 in Python by Sanam
19,219 views
+1 vote
1 answer

How to convert records from csv to a list in Python?

If you are using Python 3.x then ...READ MORE

answered Jun 25, 2019 in Python by Arvind
• 3,050 points

edited Jun 26, 2019 by Kalgi 9,851 views
0 votes
1 answer

How to create LIST from CSV file in Python?

You can use the pandas library for this which ...READ MORE

answered Jul 15, 2019 in Python by Karan
7,111 views
0 votes
0 answers
+1 vote
4 answers

how to sort a list of numbers without using built-in functions like min, max or any other function?

Yes it is possible. You can refer ...READ MORE

answered Jun 27, 2019 in Python by Arvind
• 3,050 points
188,462 views
+1 vote
4 answers

How do I remove an element from a list by index in Python?

Delete the List and its element: We have ...READ MORE

answered Jun 7, 2020 in Python by sahil
• 500 points
280,722 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP