473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Change the comma delimiter in python output to csv

20
I am trying to output a list to a csv file and the line below work fine.

I need help with changing the delimiter from the default comma(,) to tab delimited.
csv_file = open('output.csv', 'w')
csv_writer = csv.writer(csv_file)

Help very much appreciated

Kaf
Jan 4 '12 #1
8 6206
Glenton
391 Expert 256MB
This is done by setting a dialect in your csv writer. Dialect is an csv object and you can set the dialect.delimiter to '/t'.

See here
Jan 5 '12 #2
kaf3773
20
Thanks for the pointer Glenton. I got it to work by doing this
csv_writer = csv.writer(csv_file, delimiter="\t")

I am trying to use a date in an SQL where clause

prevday = datetime.date.today()-datetime.timedelta(1)
prev_day = prevday.strftime("%d-%b-%y")

and this is the SQL statement but it does not work

cur.execute("""select switch,percent_occupied from Table_name where switch =
('4DYHTE') and trunc(date_time) = %s""",("prev_day",))

I will appreciate any help

Thanks
Kaf
Jan 5 '12 #3
Glenton
391 Expert 256MB
Can you let us know what you mean by "does not work"? What's the error message? Does the prev_day need quotation marks?
Jan 5 '12 #4
kaf3773
20
Well i am trying to pass the data in the variable prev_day which is in this format dd-mon-yy to the SQL query.

without the quotes i get an invalid variable error
Jan 5 '12 #5
dwblas
626 Expert 512MB
prevday = datetime.date.today()-datetime.timedelta(1)
Print prevday to see what it contains. I don't think there is a default for timedelta so you have to specify one whatever, days=1, weeks=1, etc. From the docs
class datetime.timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
Jan 5 '12 #6
kaf3773
20
Hello Glenton,

Sorry about this but i made a mistake about the error i was getting.

When i run it with or without the quotes this is the error message i get
Traceback (most recent call last):
File "spit.py", line 9, in <module>
('4DYHTE') and trunc(date_time) = %s""",("prev_day",))
cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number

dwblas,

Concerning what is stored in prevday its the date for the previous day
>>> import datetime
>>> prevday = datetime.date.today()-datetime.timedelta(1)
>>> print prevday
2012-01-04
>>>
and i format it with this prev_day = prevday.strftime("%d-%b-%y")
to get
>>> prev_day = prevday.strftime("%d-%b-%y")
>>> print prev_day
04-Jan-12
>>>
which i want to pass to the SQL statement for the query.

Thanks for your help
Jan 5 '12 #7
kaf3773
20
i finally figured it out by doing this


cur.execute("""select switch,percent_occupied from Table_name where switch = '4DYHTE' and trunc(date_time) = ('%s')""" % (prev_day))
Jan 16 '12 #8
bvdet
2,851 Expert Mod 2GB
Congratulations on solving your problem. Thanks for the feedback!
Jan 16 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: rhungund | last post by:
Hey all... I'm submitting various form variables as arrays, so when I do try to access them when the form is submitted, i do a split on the variable and pop it into an array. for example:...
5
by: Piotr | last post by:
Is it any way to change comma to point when it is typped into the INPUT field?
2
by: silly | last post by:
/*Thanks again to thos who helped with my 'more hand written integer pow() functions (LONG POST)' query. I needed to write a function to write out integers and after looking at some stuff on...
4
by: Koray Bostancı | last post by:
Hi all, When i type python in terminal it runs the python2.3 interpreter, but i want to use 2.4 and Python2.4 package is already installed. How can i change the default python in my system? I...
1
by: ye juan | last post by:
Hi, all I have some questions to ask: 1. How can I add a standard module named "datetime" in Jython when the error happens :"Traceback (innermost last): File "C:\python\test.py", line 3, in...
7
by: fatalserpent | last post by:
Here is the basic code (yes, I know its tiny). x = print "x =", x for m in x: print m I want to modify this so it will output to a file called 1. What I want is to have that file direct its...
2
by: kirbybowl | last post by:
I have been using --#SET TERMINATOR to change the delimiter within my CLP scripts. In my situation, the CLP scripts are generated from a database of CLP commands (written by many authors), thus the...
14
by: spreadbetting | last post by:
I'm trying to split a string into an separate arrays but the data is only delimited by a comma. The actual data is one long string but the info is in a regular format and repeats after every five...
1
by: sandro dentella | last post by:
Hi, I'm having a little problem while using python mode. I'm used to hit C-c C-c and have a Python Output buffer opened with the output. Now, on Ubuntu 7.10 and 8.04, C-c C-c sends to the...
2
by: Vidhyapc | last post by:
Hi, My query gets directly executed in a report and a parameter is a varchar2 that gets the user_id passed with a comma delimiter but no quotes to the user_id( which are sometimes non-numeric), how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.