Splitting values out of a CSV Reader Python

2024/10/15 21:22:15

Here is my current code

a_reader = None
a_reader     = open('data.csv', 'rU')
a_csv_reader = csv.reader(a_reader)for row in a_csv_reader:print row
a_reader.close()count = 0
sum   = 0.0
a_reader     = open('data.csv', 'rU')
a_csv_reader = csv.reader(a_reader)
a_csv_reader.next()for row in a_csv_reader:if count != 0 and row[0] != '':sum = sum + float(row[0])count = count + 1a_reader.close()
print 'Number of lines is:',count
print 'Sum is:',sum
return listStation

This produces the results below

['1', '476050', '7709929']    
['2', '473971', '7707713']    
['3', '465676', '7691097']    
['4', '515612', '7702192']    
['5', '516655', '7704405']    
['6', '519788', '7713255']    
['7', '538466', '7683341']    
Number of lines is: 8    
Sum is: 28.0

Ok now what I want to do is to split out the value of the ID, Easting and Northing and append them to a list to create one 2d list. Is it possible to do this? If so can you provide me the code?

Answer
rows = []
for row in a_csv_reader:rows.append(row)

Will yield in rows:

[['1', '476050', '7709929']    
['2', '473971', '7707713']    
['3', '465676', '7691097']    
['4', '515612', '7702192']    
['5', '516655', '7704405']    
['6', '519788', '7713255']    
['7', '538466', '7683341']]
https://en.xdnf.cn/q/117791.html

Related Q&A

python - list variable not storing proper result in recursion

I am trying to store all possible parenthesisation of a list through recursion.Example: printRange(0,3) Answer will be [[0],[1], [2]], [[0], [1, 2]], [[0, 1], [2]], [[0, 1, 2]]I could get a right answe…

Embedding matplotlib canvas into tkinter GUI - plot is not showing up, but no error is thrown

Running the python python script below does not show the embedded matplotlib plot. However it also throws no error message. Upon running the script, it is supposed to display a GUI displaying 4 buttons…

Can I get rid of this b character in my print statement? [duplicate]

This question already has answers here:What does a b prefix before a python string mean?(2 answers)Closed 7 years ago.Im wondering what this b charcter is and why its appearing. Im also wondering if I…

Python tkinter: configure multiple labels with a loop

I have a window with multiple labels. Instead of configuring each label individually, I want to use a for loop to configure them.Basically, what I get from the below code is all labels are showing the …

How do you create a sitemap index in Django?

The Django documentation is very minimal and I cant seem to get this to work.Currently I have 3 individual sitemaps, and I would like to create a sitemap index for them: (r^sitemap1\.xml$, django.contr…

Numpy Append to Array

I’m building a project for the Raspberry Pi that turns a relay on and off random times in a specific time window. To manage the time slots, I want to use a two-dimensional array that’s generated dail…

Output of numpy.diff is wrong

heres my problem: I am trying to use numpy to compute (numerical) derivatives, but I am finding some problems in the value the function numpy.diff returns (and numpy.gradient as well). What I find is t…

Calculate Fibonacci numbers up to at least n

I am trying to make a function that allows a user to input a number and the result will be a list containing Fibonacci numbers up to the input and one above if the input is not in the series. For examp…

IEC 62056-21, implement the protocol over a gsm connection

The protocol IEC 62056:21 tells us how to deal with enegy meters, its quite easy!The part where I am stuck is the implementation over a GSM data channel. Normally I would set things like:300 baudrate …

Cannot install plyfile in Anaconda

When u=i try to run the commandconda install plyfilein windows command promptFetching package metadata ...........PackageNotFoundError: Package not found: Package missing in current win-64 channels: -…