CSV writing cannot be done - python

CSV writing cannot be done.The list of "li_result" has data result and I want to write this data in csv file.
This is the code
fp = open('dataResult.csv', 'w')
w = csv.writer(fp, delimiter=',')
csvwrite = unicode(li_result)
csvwrite_result = csvwrite.encode('sjis')
w.writerow(csvwrite_result)
But dataResult.csv is empty. Nothing error happen so I do not know what is wrong.
And I want to write the data in sjis code in csv file.(Now I use python2.7 so unicode is used to write letters,right?) I deleted these codes
csvwrite = unicode(li_result)
csvwrite_result = csvwrite.encode('sjis')
Still nothing is written.
What should I do to fix this?
Sample codes
fp = open(CSV_FILE_NAME_ACCOUNT, 'aw')
w = csv.writer(fp, delimiter=',')
title = 'abc'
name = 'hoge'
time = '2010-04-20 0:0:0'
u_title = unicode(title)
u_name = unicode(name)
u_time = unicode(time)
s_title = u_title.encode('sjis')
s_name = u_name.encode('sjis')
s_time = u_time.encode('sjis')
list = [s_title, s_name, s_time]
w.writerow(list)

import csv
fp = open('system path to your file on which data to read', 'w')
w = csv.writer(fp, delimiter=',')
title = 'abc'
name = 'hoge'
time = '2010-04-20 0:0:0'
list = [title, name, time]
w.writerow(list)

Related

Csv file only store single row pf data using python

I scrape a website and try to store it into Csv format but when i did it is store only single row of data.
how to write multiples Row of data in csv.
for lis in lists:
title = lis.find('a', class_="title").text
tag = lis.find('span', class_="etc-mark").text
datetime = lis.find('span', class_="datetime").text
address = lis.find('div', class_="middle-xs").text
img = lis.find('span', class_="https://thecryptobasic.com")
data = [title, tag, datetime,address,img]
print(data)
# create the csv writer
writer = csv.writer(f)
# write a row to the csv file
writer.writerow(header)
writer.writerow(data)
# close the file
f.close()
data gets overwritten in every iteration, so after the loop it contains the data from the last iteration only.
Initialize data before the loop, the append to it in every iteration.
Use writerows instead of writerow.
data = []
for lis in lists:
title = lis.find('a', class_="title").text
tag = lis.find('span', class_="etc-mark").text
datetime = lis.find('span', class_="datetime").text
address = lis.find('div', class_="middle-xs").text
img = lis.find('span', class_="https://thecryptobasic.com")
data.append([title, tag, datetime, address, img])
...
writer.writerows(data)
while refactoring this piece of code, it will also better to let with manage the opening and closing of the file:
...
with open(..., 'w') as f:
writer = csv.writer(f)
writer.writerow(header)
writer.writerows(data)
We can even merge the write calls by pre-initializing data with header:
data = [header]
for lis in lists:
title = lis.find('a', class_="title").text
tag = lis.find('span', class_="etc-mark").text
datetime = lis.find('span', class_="datetime").text
address = lis.find('div', class_="middle-xs").text
img = lis.find('span', class_="https://thecryptobasic.com")
data.append([title, tag, datetime, address, img])
with open(..., 'w') as f:
writer = csv.writer(f)
writer.writerows(data)

Python CSV SQL rows not printing

I am trying to write to each columns the results of my queries, however when I run my below script it seems to format with the data not appearing in the column next to it displaying all the rows.
results = []
results2 = []
results3 = []
results4 = []
results5 = []
results6 = []
cur.execute(dbQuery)
results.extend(cur.fetchall())
cur.execute(dbQuery2)
results2.extend(cur.fetchall())
cur.execute(dbQuery3)
results3.extend(cur.fetchall())
cur.execute(dbQuery4)
results4.extend(cur.fetchall())
cur.execute(dbQuery5)
results5.extend(cur.fetchall())
cur.execute(dbQuery6)
results6.extend(cur.fetchall())
with open("out.csv", "wb") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Query1', 'Query2', 'Query3', 'Query4', 'Query5', 'Query6'])
csv_writer.writerow(results, results2, results3, results4, results5, results6)
You have to iterate the results of all the query, then you need to write the CSV file. I assumed all the results from DB like below.
import csv
results = [1,2,3,4]
results2 = [11,12,13,14]
results3 = [21,22,23,24]
results4 = [31,32,33,34]
results5 = [41,42,43,44]
results6 = [51,52,53,54]
with open('out.csv', 'wb') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow(['Query1', 'Query2', 'Query3', 'Query4', 'Query5', 'Query6'])
csv_writer.writerows(zip(*[results, results2, results3, results4, results5, results6]))

write output of for loop in csv python

i want to write the result of for loop which is PMID = Id of litrature ,Date = date of publication ,title = title of article,Abstract = abtract of artilce in csv file but it is saving only one element of the output no all
import numpy as np
from Bio import Entrez
from Bio import Medline
import csv
import pandas as pd
Entrez.email = "shayezkarimcide#gmail.com"
handle = Entrez.esearch(db="pmc",
term = "Antimicrobial resistance Drug Resistance",
rettype = "medline",retmode = "txt",
retmax= "200",sort = "pub date")
result = Entrez.read(handle)
handle.close()
Id = result ['IdList']
print (Id)
handle2 = Entrez.efetch(db="pmc",
id=Id, rettype="medline",
retmode="text")
records = Medline.parse(handle2)
header = ['ID','Date','Title','Abstract']
for result in records :
PMID = result['PMID']
Abstract = result['AB']
title = result['TI']
Date = result['DP']
print (PMID,Date,title,Abstract)
fields = [PMID, title,Date,Abstract]
rows = [PMID,Date,title,Abstract]
with open ('/home/shayez/Desktop/karim.csv','wt') as csvfile:
writer = csv.writer(csvfile, delimiter ="\t" )
writer.writerow(header)
writer.writerow(rows)
handle2.close()
You are opening the file, writing and closing it inside the loop (the with makes sure the file is closed after the with's scope is done) so it is replacing the previous file for each element in the loop.
Try opening the file only once, before the loop:
with open ('/home/shayez/Desktop/karim.csv','wt') as csvfile:
writer = csv.writer(csvfile, delimiter ="\t" )
writer.writerow(header)
for result in records :
PMID = result['PMID']
Abstract = result['AB']
title = result['TI']
Date = result['DP']
print (PMID,Date,title,Abstract)
fields = [PMID, title,Date,Abstract]
rows = [PMID,Date,title,Abstract]
writer.writerow(rows)

Why Python don't save csv results

I don't understand why Python don't save results correctly, while it prints it correct. Code look like this:
import csv
with open("dataset_1.csv", "r") as WBI:
data = csv.reader(WBI, delimiter = ";")
data = list(data)
header = data[0]
data = data[1:]
WaterBandIndex = []
for row in data:
WaterBandIndex.append(float(row[54])/float(row[83]))
print (WaterBandIndex)
with open("WBI.csv", "w+") as WBI:
csvwriter = csv.writer(WaterBandIndex, delimiter = "|", lineterminator = "\n")
csvwriter.writerows(WaterBandIndex)
Printed results are correct, but saves to csv nothing.
I'm green in programming.
The code should work if your variable WaterBandIndex is not empty.
import csv
WaterBandIndex = ['1','2','3']
with open("WBI.csv", "w") as f:
csvwriter = csv.writer(f , delimiter = "|", lineterminator = "\n")
csvwriter.writerows(WaterBandIndex)

trying to read xlrd, extract data, and write csv

I am trying to read an excel file, extract some data, and write it out as a csv. This is pretty new to me and I'm messing up somewhere: I keep getting an empty csv. I'm sure I'm missing something very basic, but darned if I can see it. Here is the code:
```
import xlrd
import os
import csv
from zipfile import ZipFile
import datetime
datafile = "./2013_ERCOT_Hourly_Load_Data.xls"
outfile = "./2013_Max_Loads.csv"
def parse_file(datafile):
workbook = xlrd.open_workbook(datafile)
sheet = workbook.sheet_by_index(0)
data = None
outputlist = []
for col in range(1, sheet.ncols):
cv = sheet.col_values(col, start_rowx=1, end_rowx=None)
header = sheet.cell_value(0,col)
maxval = max(cv)
maxpos = cv.index(maxval) + 1
maxtime = sheet.cell_value(maxpos, 0)
realtime = xlrd.xldate_as_tuple(maxtime, 0)
year = realtime[0]
month = realtime[1]
day = realtime[2]
hour = realtime[3]
data = [
'Region:', header,
'Year:', year,
'Month:', month,
'Day:', day,
'Hour:', hour,
maxpos,
maxtime,
realtime,
maxval,
]
path = "./2013_Max_Loads.csv"
return outputlist
def save_file(data, filename):
with open(filename, "wb") as f:
writer = csv.writer(f, delimiter='|')
for line in data:
writer.writerow(line)
parse_file(datafile)
save_file(parse_file(datafile),"2013_Max_Loads.csv")
You declare outfile but you don't use it
You aren't passing a directory (path) for the file to be saved in.
I also think that calling parse_file twice might be messing you up. Just pass the filename and call it from within the save_file function.
I also found that you were returning output list as a blank list.
So here, try this. I will assume your xlrd commands are correct, because I have not personally used the module.
import csv
import xlrd
def parse_file(datafile):
workbook = xlrd.open_workbook(datafile)
sheet = workbook.sheet_by_index(0)
outputlist = []
outputlist_append = outputlist.append
for col in range(1, sheet.ncols):
cv = sheet.col_values(col, start_rowx=1, end_rowx=None)
header = sheet.cell_value(0,col)
maxval = max(cv)
maxpos = cv.index(maxval) + 1
maxtime = sheet.cell_value(maxpos, 0)
realtime = xlrd.xldate_as_tuple(maxtime, 0)
year = realtime[0]
month = realtime[1]
day = realtime[2]
hour = realtime[3]
data = [
'Region:', header,
'Year:', year,
'Month:', month,
'Day:', day,
'Hour:', hour,
maxpos,
maxtime,
realtime,
maxval,
]
outputlist_append(data)
return outputlist
def save_file(data, filename):
parse_file(data)
with open(filename, 'wb') as f:
writer = csv.writer(f, delimiter='|')
for line in data:
writer.writerow(line)
return
datafile = "./2013_ERCOT_Hourly_Load_Data.xls"
outfile = "./2013_Max_Loads.csv"
save_file(datafile, outfile)
UPDATE: Edit in code in function save_file() to implement #wwii's suggestion.
Try substituting the new save_file() below:
def save_file(data, filename):
parse_file(data)
with open(filename, 'wb') as f:
wr = csv.writer(f, delimiter='|')
wr.writerows(data)
return
Also, change the variable (you used writer) to something like wr. You really want to avoid any possible conflicts with having a variable with the same name as a method, a function, or class you are calling.

Categories

Resources