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 15, 2020 in Python by anonymous
• 120 points

edited Sep 16, 2020 by Gitika 888 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,880 points

Related Questions In Python

0 votes
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
501 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,540 points
578 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
2,112 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
2,414 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
• 2,980 points

edited Jun 26, 2019 by Kalgi 3,696 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
4,334 views
0 votes
0 answers
0 votes
4 answers

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

I guess you are trying to do ...READ MORE

answered Dec 11, 2020 in Python by Rajiv
• 8,880 points
25,297 views
0 votes
4 answers

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

1886 Use del and specify the index of the element ...READ MORE

answered Dec 11, 2020 in Python by Gitika
• 65,850 points
48,423 views