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,,,,,,,,,
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)
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]