Python output to csv file - python

I have a question in output to a csv file in Python:
Code as below:
import numpy as np
import scipy.stats as stats
from scipy.stats import poisson, norm
# Read the csv file and obtain corresponding parameter mu, cs and co.
import csv
with open('r1.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print row
mu = row[0]
cs = row[1]
co = row[2]
mu = float(mu)
cs = float(cs)
co = float(co)
# Generate a Poisson Distribution and
G = poisson(mu)
p = G.pmf(np.arange(3*mu))
# Define Z(Q) for the total cost estimation
def Z(Q):
ES = sum(i*p[i] for i in range(len(p)))
return cs*max((Q-ES), 0) + co*max((ES-Q), 0)
# Obtain Qstar
Qstar = np.ceil(poisson.ppf(co/(cs+co), mu))-1
Qstar = int(np.float64(Qstar).item())
This part of code works fine for me and I got Qstar = 5 in this simple example. How could I output it to a csv file?
Order_Number
5
I have the following code to call Qstar:
with open('test.csv', 'wb') as fp:
a = csv.writer(fp, delimiter=',')
data = [['Order_Number'],['Qstar']]
a.writerows(data)
But it seems I only obtain
Order_Number
Qstar
The nhow could I call 'Qstar' correctly?
Thank you!

Try this:
with open('test.csv', 'wb') as fp:
a = csv.writer(fp, delimiter=',')
data = [['Order_Number'],[Qstar]]
a.writerows(data)

By using single quotes around Qstar you are telling Python to create a string with the value 'Qstar'. In order to output the value of the variable Qstar, just don't use the single quotes:
with open('test.csv', 'wb') as fp:
a = csv.writer(fp, delimiter=',')
data = [['Order_Number'],[Qstar]]
a.writerows(data)

That's because you feed data array with strings instead of numbers. Remove the single quotes from Qstar and Order_number.

Related

Function parameters iterate over a csv file values

i have this function
def audience(lat,lon,ids):
buff = buff_here(lat,lon)[-1]
count_visitas = []
for visitas in glob.glob(path): ......
df = pd.DataFrame(count_visitas, columns =['Visitas'])
df.to_csv(f'output/visitas_simi_{ids}.csv', index = False)
return count_visitas
I can't post the complete code here due to work issues, but it's works perfectly fine if i pass this parameters
audience(-33.51133739,-70.7558227,'CL0008')
Now, i have this csv and want to iterate over the rows of lat, lon and id as a parameter of the function. Any help, please? :c
You would need to bring the csv in with csv.DictReader and then you can call the desired columns:
csv_file = csv.DictReader(open(file, 'rU'))
for row in csv_file:
count_visitas = audience(row['lat'],row['lon'],row['ids'])
This code should work:
import csv
with open("names.csv", "r") as csv_file:
csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
lat = line["first_name"]
lon = line["last_name"]
ids = line["email"]
audience(lat, lon, ids)

Sentiment analysis on csv file Python 3.8

I have a csv file where I wish to perform a sentiment analysis on this dataset containing survey data.
So far this is what I have tried (thanks to Rupin from a previous question!):
import csv
from collections import Counter
with open('myfile.csv', 'r') as f:
reader = csv.reader(f, delimiter='\t')
alist = []
iterreader = iter(reader)
next(iterreader, None)
for row in iterreader:
clean_rows = row[0].replace(",", " ").rsplit()
alist.append(clean_rows)
word_count = Counter(clean_rows)
mostWcommon = word_count.most_common(3)
print(mostWcommon)
The output is nearly okay, the only problem that I have is that Python is splitting in different rows of a list, hence I have something like this as my output:
['experienced', 1]
['experienced, 1]
['experienced, 1]
I wish to split everything in one row so that I can have the real word frequency... Any suggestions?
Thanks!
You are creating a new Counter for each row and printing only that result. If you want a total count, you can create the counter outside the rows loop and update it with data from each row:
import csv
from collections import Counter
with open('myfile.csv', 'r') as f:
reader = csv.reader(f, delimiter='\t')
alist = []
iterreader = iter(reader)
next(iterreader, None)
c = Conter()
for row in iterreader:
clean_rows = row[0].replace(",", " ").rsplit()
alist.append(clean_rows)
c.update(clean_rows)
mostWcommon = word_count.most_common(3)
print(mostWcommon)

Write CSV file using Python with the help of a csv dictionary / nested csv dictionary

I am having a csv file and i want to write it to another csv file.
It's a bit complicated than it seems. Hoping someone to correct my code and rewrite it, so that i can get the desired csvfile. I am using both versions python 2 and 3.
mycsvfile:
id,field1,point_x,point_y,point_z
a1,x1,10,12,3
b1,x2,20,22,5
a2,x1,25,17,7
a1,x2,35,13,3
a1,x5,15,19,9
b1,x1,65,11,2
b2,x5,50,23,1
b2,x1,75,17,7
c1,x2,70,87,2
c2,x1,80,67,4
c3,x2,85,51,6
Figure: mycsvfile
Mycode:
import os
import csv
import collections
from csv import DictWriter
with open(r'C:\Users\Desktop\kar_csv_test\workfiles\incsv.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
my_dict = collections.defaultdict(dict)
for row in reader:
my_dict[row[0]][row[1]] = [row[2],row[3],row[4]]
print (my_dict)
with open(r'C:\Users\Desktop\kar_csv_test\workfiles\outcsv.csv','w', newline='') as wf:
fieldnames = ['id', 'x1(point_x)', 'x1(point_y)', 'x1(point_z)', 'x2(point_x)', 'x2(point_y)', 'x2(point_z)'] # >>>>>>etc, till x20(point_x), x20(point_y), x20(point_z)
my_write = csv.DictWriter(wf, fieldnames = fieldnames, delimiter = ',')
my_write.writeheader()
Desired output as csv file:
id,x1(point_x),x1(point_y),x1(point_z),x2(point_x),x2(point_y),x2(point_z)
a1,10,12,3,35,13,3,
a2,25,17,7,,,,
b1,65,11,2,20,22,5,
b2,75,17,7,,,,
c1,,,,70,87,2,
c2,80,67,4,,,,
c3,,,,85,51,6,
Figure: Desiredcsvfile
This answer is for Python3 only. The csv module has a very different interface between Python2 and Python3 and writing compatible code is beyond what I am ready to do here.
Here, I would compute the fieldnames list, and compute each row on the same pattern:
...
with open(r'C:\Users\Desktop\kar_csv_test\workfiles\outcsv.csv','w', newline='') as wf:
fieldnames = ['id'] + ['x{}(point_{})'.format(i, j)
for i in range(1, 6) for j in "xyz"] # only up to x5 here
my_write = csv.DictWriter(wf, fieldnames = fieldnames, delimiter = ',')
my_write.writeheader()
for k, v in my_dict.items():
row = {'x{}(point_{})'.format(i, k):
v.get('x{}'.format(i), ('','',''))[j] # get allows to get a blank triple is absent
for i in range(1,6) for j,k in enumerate("xyz")}
row['id'] = k # do not forget the id...
my_write.writerow(row)
With your example data, it gives:
id,x1(point_x),x1(point_y),x1(point_z),x2(point_x),x2(point_y),x2(point_z),x3(point_x),x3(point_y),x3(point_z),x4(point_x),x4(point_y),x4(point_z),x5(point_x),x5(point_y),x5(point_z)
a1,10,12,3,35,13,3,,,,,,,15,19,9
b1,65,11,2,20,22,5,,,,,,,,,
a2,25,17,7,,,,,,,,,,,,
b2,75,17,7,,,,,,,,,,50,23,1
c1,,,,70,87,2,,,,,,,,,
c2,80,67,4,,,,,,,,,,,,
c3,,,,85,51,6,,,,,,,,,

Import csv, manipulate and export to csv

I have a csv file, which contains some rows and columns. I need to pick some specific values out of that table, enhance them and then export them to another csv file.
My Python 3 code looks like this:
import csv
from urllib.parse import quote
afn='https://r.refinedads.com/bs-mapping.php?v=a1&aid=7909&oid=1031&url='
zx='https://r.refinedads.com/bs-mapping.php?v=z1&aid=3814&oid=1031&zanpid=##zxClickID##&userid=##UserID##&url='
zxpn='https://r.refinedads.com/bs-mapping.php?v=z1&aid=3840&oid=1031&zanpid=##zxClickID##&userid=##UserID##&url='
la='https://r.refinedads.com/bs-mapping.php?aid=3843&oid=1031&c1=leadalliance&c2=XXXXX&c3=default&clid=YYYYY&url='
tv='https://r.refinedads.com/bs-mapping.php?aid=7639&oid=1031&c1=leadalliance&c2=XXXXX&c3=default&clid=YYYYY&url='
with open('input_csv.csv', 'r', newline='') as csvfile:
matrixreader = csv.DictReader(csvfile)
for row in matrixreader:
va = afn+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vzx = zx+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vzxpn = zxpn+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vla = la+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vtv = tv+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
liste = [[row['Name'], va, vzx, vzxpn, vla, vtv]]
print(liste)
with open('output.csv', 'w', newline='') as csvfile:
a = csv.writer(csvfile, delimiter=',')
a.writerows(liste)
As an output I just receive the last row of input.csv exported to output.csv. It seems to me as if the code just overwrites the first row.
input.csv looks like this:
*Name,Promocode,Link
BASE Plus + iPhone 7,ASDFNOWEDF,base.de/base-plus
BASE Pro + iPhone 7,JBONEDGASD,base.de/base-pro
BASE Light + iPhone 7,NAFODSFNTE,base.de/base-light
BASE Pur + iPhone 7,NAEWRIONF,base.de/base-pur*
Thank you very much in advance for your help,
Cheers,
Franz
The one problem is that you try to update your liste variable outside of the read loop. The other problem is that you re-assign to the liste variable, instead of appending to it.
However, you don't need to collect all rows in a list and then write them to the output. You can do that by writing each row the moment you read and form it.
with open('input_csv.csv', 'r', newline='') as csvfile_in:
with open('output.csv', 'w', newline='') as csvfile_out:
matrixreader = csv.DictReader(csvfile_in)
a = csv.writer(csvfile_out, delimiter=',')
for row in matrixreader:
va = afn+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vzx = zx+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vzxpn = zxpn+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vla = la+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vtv = tv+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
a.writerow([row['Name'], va, vzx, vzxpn, vla, vtv])
This is way more efficient and memory friendly, especially if the input file is very large.
Try putting the liste assignment inside the if loop -
import csv
from urllib.parse import quote
afn='https://r.refinedads.com/bs-mapping.php?v=a1&aid=7909&oid=1031&url='
zx='https://r.refinedads.com/bs-mapping.php?v=z1&aid=3814&oid=1031&zanpid=##zxClickID##&userid=##UserID##&url='
zxpn='https://r.refinedads.com/bs-mapping.php?v=z1&aid=3840&oid=1031&zanpid=##zxClickID##&userid=##UserID##&url='
la='https://r.refinedads.com/bs-mapping.php?aid=3843&oid=1031&c1=leadalliance&c2=XXXXX&c3=default&clid=YYYYY&url='
tv='https://r.refinedads.com/bs-mapping.php?aid=7639&oid=1031&c1=leadalliance&c2=XXXXX&c3=default&clid=YYYYY&url='
with open('input_csv.csv', 'r', newline='') as csvfile:
matrixreader = csv.DictReader(csvfile)
liste=[] # initialize here
for row in matrixreader:
va = afn+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vzx = zx+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vzxpn = zxpn+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vla = la+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
vtv = tv+quote(row['Link']+'&prf[pid]=[PARTNERID]&prf[a]=[AFFID]'+'-'+row['Promocode'])
liste = liste.append([[row['Name'], va, vzx, vzxpn, vla, vtv]]) # keep appending
print(liste)
with open('output.csv', 'w', newline='') as csvfile:
a = csv.writer(csvfile, delimiter=',')
a.writerows(liste)

How can I get a specific field of a csv file?

I need a way to get a specific item(field) of a CSV. Say I have a CSV with 100 rows and 2 columns (comma seperated). First column emails, second column passwords. For example I want to get the password of the email in row 38. So I need only the item from 2nd column row 38...
Say I have a csv file:
aaaaa#aaa.com,bbbbb
ccccc#ccc.com,ddddd
How can I get only 'ddddd' for example?
I'm new to the language and tried some stuff with the csv module, but I don't get it...
import csv
mycsv = csv.reader(open(myfilepath))
for row in mycsv:
text = row[1]
Following the comments to the SO question here, a best, more robust code would be:
import csv
with open(myfilepath, 'rb') as f:
mycsv = csv.reader(f)
for row in mycsv:
text = row[1]
............
Update: If what the OP actually wants is the last string in the last row of the csv file, there are several aproaches that not necesarily needs csv. For example,
fulltxt = open(mifilepath, 'rb').read()
laststring = fulltxt.split(',')[-1]
This is not good for very big files because you load the complete text in memory but could be ok for small files. Note that laststring could include a newline character so strip it before use.
And finally if what the OP wants is the second string in line n (for n=2):
Update 2: This is now the same code than the one in the answer from J.F.Sebastian. (The credit is for him):
import csv
line_number = 2
with open(myfilepath, 'rb') as f:
mycsv = csv.reader(f)
mycsv = list(mycsv)
text = mycsv[line_number][1]
............
#!/usr/bin/env python
"""Print a field specified by row, column numbers from given csv file.
USAGE:
%prog csv_filename row_number column_number
"""
import csv
import sys
filename = sys.argv[1]
row_number, column_number = [int(arg, 10)-1 for arg in sys.argv[2:])]
with open(filename, 'rb') as f:
rows = list(csv.reader(f))
print rows[row_number][column_number]
Example
$ python print-csv-field.py input.csv 2 2
ddddd
Note: list(csv.reader(f)) loads the whole file in memory. To avoid that you could use itertools:
import itertools
# ...
with open(filename, 'rb') as f:
row = next(itertools.islice(csv.reader(f), row_number, row_number+1))
print row[column_number]
import csv
def read_cell(x, y):
with open('file.csv', 'r') as f:
reader = csv.reader(f)
y_count = 0
for n in reader:
if y_count == y:
cell = n[x]
return cell
y_count += 1
print (read_cell(4, 8))
This example prints cell 4, 8 in Python 3.
There is an interesting point you need to catch about csv.reader() object. The csv.reader object is not list type, and not subscriptable.
This works:
for r in csv.reader(file_obj): # file not closed
print r
This does not:
r = csv.reader(file_obj)
print r[0]
So, you first have to convert to list type in order to make the above code work.
r = list( csv.reader(file_obj) )
print r[0]
Finaly I got it!!!
import csv
def select_index(index):
csv_file = open('oscar_age_female.csv', 'r')
csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
l = line['Index']
if l == index:
print(line[' "Name"'])
select_index('11')
"Bette Davis"
Following may be be what you are looking for:
import pandas as pd
df = pd.read_csv("table.csv")
print(df["Password"][row_number])
#where row_number is 38 maybe
import csv
inf = csv.reader(open('yourfile.csv','r'))
for row in inf:
print row[1]

Categories