I have started learning python with great enthusiasm and managed to get through the procedures, functions and bit of lists. And feel ready to take the learning forward by building a useful application.
I would like the user to export an excel file into csv with just one row of numbers, some of them negative, positive and zeros. And want to calculate the average of positive numbers and average of negative numbers.
The calculating bit isn't the problem, its the importing csv bit. I've been reading the python documentation but looked confusing.
Please get me started on importing a csv file and summing the row of numbers. Thank you!
This should get you started.
import csv
with open("file.csv", "rb") as ins:
for row in csv.reader(ins):
print sum(map(int, row))
The with statement is a way to make this exception-safe. In most cases, the following is good enough:
import csv
ins = open("file.csv", "rb")
for row in csv.reader(ins):
print sum(map(int, row))
ins.close()
The above solutions don't work with Python 3. Instead, try this:
import csv
with open("test.csv", "r") as ins:
for row in csv.reader(ins):
print(sum(map(int, row)))
Related
I have an asignment in which I need to imput random grades of different students in a csv file using Python 3, and get the average of each student(the average thing and how to get the random grades, I know how to do it), the thing is that I don't know how to write the grades on those specific columns and rows(highlighted ones).
Highlighted area is the space in which I need to write random grades:
Is there anyway that this can be done? I'm fairly new to programming and Python 3, and as far as I've read, specifics cells can't be changed using normal means.
csv module doesn't have functions to modify specific cells.
You can read rows from original file, append grades and write modified rows to new file:
import random
import csv
inputFile = open('grades.csv', 'r')
outputFile = open('grades_out.csv', 'w')
reader = csv.reader(inputFile)
writer = csv.writer(outputFile)
for row in reader:
grades = row.copy()
for i in range(5):
grades.append(random.randint(1, 5))
writer.writerow(grades)
inputFile.close()
outputFile.close()
Then you can delete original file and rename new file (it is not good to read the whole original file to a variable, close it, open it again in writing mode and then write data, because it can be big).
I'm currently trying to work with data about artists, their songs, and the lyrics. I have csv with the artist, the song name, and the lyrics in that order. I'm trying to split it so that I have each thing separate however the lyrics keep getting split whenever there is a new line. I've tried using this.
fp = open('songdata_test.csv', 'r')
for line in fp:
line_lst = line.split(',')
However that just returned the error previously described. Does anyone know how to split this csv so that the lyrics do not get split?
Edit: Example of what I'm trying to split.
Adele,All I Ask,"[Verse 1]
I will leave my heart at the door
I won't say a word
They've all been said before, you know..."
Bob Dylan,4Th Time Around,"When she said, ""Don't waste your words, they're
just lies,""
I cried she was deaf.
And she worked on my face until breaking my eyes,
Then said, ""What else you got left?""
It was then that I got up to leave..."
Parsing a csv with lyrics has some non-trivial problems that are difficult to handle by yourself (I can see from your edition that you already figured this out). In particular, columns delimited by quotes and new lines or commas inside the data itself are difficult to parse and there are modules already designed for such tasks.
I suggest trying with python's csv.reader or, better, with pandas.
Using csv.reader
From the documentation:
import csv
with open('songdata_test.csv') as csvfile:
reader= csv.reader(csvfile, delimiter=',', quotechar='"') # These are the defaults, I'm just showing the explicitly. This is equivalent to csv.reader(csvfile)
for row in reader:
print(', '.join(row))
Using pandas
import pandas as pd
df = pd.read_csv('songdata_test.csv')
This will return a pandas DataFrame object and handling it correctly will involve some learning, but if you use python and csvs with python I strongly suggest giving it a try.
This is my code that adds the data to the CSV file known as studentScores.csv
myfile = open("studentScores.csv", "a+")
newRecord = Score, Name, Gender, FormGroup, Percentage
myfile.write(str(newRecord))
myfile.write("\n")
myfile.close()
As a part of my task, I need to alphabetise the data in the CSV, I have searched, and searched for a solution, but I am unable to find a working solution for me. I am pretty new to Python, so the simplest solution will be appreciated.
import csv
from operator import itemgetter
with open('studentScores.csv', 'r') as f:
data = [line for line in csv.reader(f)]
newRecord = [Score, Name, Gender, FormGroup, Percentage]
data.append(newRecord)
data.sort(key=itemgetter(1)) # 1 being the column number
with open('studentScores.csv', 'w') as f:
csv.writer(f).writerows(data)
First of all, this uses functions from the csv module for properly parsing and creating CSV syntax. Secondly, it reads all existing entries into data, appends the new record, sorts all records, then dumps them back to the file.
If you're using a header row in your CSV file to add names to columns, look at DictReader and DictWriter, that would allow you to handle columns by name, not number (e.g. in the sorting step).
I am in the process of creating a simple random number generator in python for a school project. This is what I have so far:
import random
amnt = input('Please enter the amount of numbers you would like:')
for i in range(0,amnt):
x = random.randint(0,100000000)
print x
This has the desired result, it generates a set amount of random numbers based on the user input. The problem I need to solve now is how to export the numbers generated into one CSV file so that they can be analysed. I believe that the CSV module needs to be imported and implemented but I am not sure how to do this. I am trying to analyze the effectiveness of the random module in order to write an essay so being able to use excel to sort and filter the numbers would be very helpful. Any changes or modifications to the code would also be very much appreciated.
You just need a one line code to convert a variable to csv format.
Let me know if this does not work.
If the code works for you please rate the answer.
x.to_csv('file_name.csv')
No, you don't really the csv module for a case this simple. You just need to create a text file in which the values are separated by commas. (Hence the name, Comma-Separated Values, CSV).
Try this:
import random
amnt = int(raw_input('Please enter the amount of numbers you would like:'))
data = (random.randint(0,100000000) for _ in range(amnt))
data = (str(datum) for datum in data)
data = ','.join(data) + '\n'
with open("random.csv", "w") as fp:
fp.write(data)
import random
import csv
amnt = input('Please enter the amount of numbers you would like:')
ofile = open('ttest.csv', "wb")
writer = csv.writer(ofile, delimiter=',')
for i in range(0,amnt):
x = random.randint(0,100000000)
writer.writerow([x])
ofile.close()
Might be a quick solution to your problem. writerow will write a row to your csv. Since you want to open it in excel I wrote one number/row, so you can order it based on the column.
However, you could also sort the numbers programatically without having to use excel. As some already mentioned CSV is especially aimed for storing data structures.
More info can be found in the csv module documentation
I am trying to use Python's built-in filter function in order to extract data from certain columns in a CSV. Is this a good use of the filter function? Would I have to define the data in these columns first, or would Python somehow already know which columns contain what data?
Since python boasted "batteries included", for most the everyday situations, someone might already provided a solution.
CSV is one of them, there is built-in csv module
Also tablib is a very good 3rd-party module especially you're dealing with non-ascii data.
For the behaviour you described in the comment, this will do:
import csv
with open('some.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
row.pop(1)
print ", ".join(row)
The filter function is intended to select from a list (or in general, any iterable) those elements which satisfy a certain condition. It's not really intended for index-based selection. So although you could use it to pick out specified columns of a CSV file, I wouldn't recommend it. Instead you should probably use something like this:
with open(filename, 'rb') as f:
for record in csv.reader(f):
do_something_with(record[0], record[2])
Depending on what exactly you are doing with the records, it may be better to create an iterator over the columns of interest:
with open(filename, 'rb') as f:
the_iterator = ((record[0], record[2]) for record in csv.reader(f))
# do something with the iterator
or, if you need non-sequential processing, perhaps a list:
with open(filename, 'rb') as f:
the_list = [(record[0], record[2]) for record in csv.reader(f)]
# do something with the list
I'm not sure what you mean by defining the data in the columns. The data are defined by the CSV file.
By comparison, here's a case in which you would want to use filter: suppose your CSV file contains numeric data, and you need to build a list of the records in which the numbers are in strictly increasing order within the row. You could write a function to determine whether a list of numbers is in strictly increasing order:
def strictly_increasing(fields):
return all(int(i) < int(j) for i,j in pairwise(fields))
(see the itertools documentation for a definition of pairwise). Then you can use this as the condition in filter:
with open(filename, 'rb') as f:
the_list = filter(strictly_increasing, csv.reader(f))
# do something with the list
Of course, the same thing could, and usually would, be implemented as a list comprehension:
with open(filename, 'rb') as f:
the_list = [record for record in csv.reader(f) if strictly_increasing(record)]
# do something with the list
so there's little reason to use filter in practice.