473,396 Members | 1,917 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,396 software developers and data experts.

csv - how to parse a string, not a file?

I've using the csv module to parse a string, not a file.
So how do I make a string look like a file so I can call csv.reader()
with it?

An an example, I'd like to call something like:

csvReader = csv.reader("1,2,3,4")

... but this direct attempt won't work as it expects a file or a handle...

thanks
alex
Jul 18 '05 #1
7 14615
import StringIO

csvReader = csv.reader(StringIO.StringIO("1,2,3,4"))

On 7/10/04 1:21 PM, in article
Tk*******************@fe1.news.blueyonder.co.uk, "Alex Hunsley"
<la**@tardis.ed.ac.molar.uk> wrote:
I've using the csv module to parse a string, not a file.
So how do I make a string look like a file so I can call csv.reader()
with it?

An an example, I'd like to call something like:

csvReader = csv.reader("1,2,3,4")

.. but this direct attempt won't work as it expects a file or a handle...
thanks
alex


Jul 18 '05 #2
On Sat, 10 Jul 2004 19:21:23 GMT, Alex Hunsley
<la**@tardis.ed.ac.molar.uk> wrote:
I've using the csv module to parse a string, not a file.
So how do I make a string look like a file so I can call csv.reader()
with it?

An an example, I'd like to call something like:

csvReader = csv.reader("1,2,3,4")

.. but this direct attempt won't work as it expects a file or a handle...


what is cStringIO?

--
John Lenton (jl*****@gmail.com) -- Random fortune:
bash: fortune: command not found
Jul 18 '05 #3
John Lenton wrote:
On Sat, 10 Jul 2004 19:21:23 GMT, Alex Hunsley
<la**@tardis.ed.ac.molar.uk> wrote:
I've using the csv module to parse a string, not a file.
So how do I make a string look like a file so I can call csv.reader()
with it?

An an example, I'd like to call something like:

csvReader = csv.reader("1,2,3,4")

.. but this direct attempt won't work as it expects a file or a handle...

what is cStringIO?


I'm not sure, but if you google there are answers:

http://www.google.com/search?hl=en&i...=Google+Search

alex
Jul 18 '05 #4
Foovoid Null wrote:
import StringIO

csvReader = csv.reader(StringIO.StringIO("1,2,3,4"))


Much thanks! That does the job nicely..
alex
Jul 18 '05 #5
On Sat, 10 Jul 2004 19:50:25 GMT, Alex Hunsley
<la**@tardis.ed.ac.molar.uk> wrote:
John Lenton wrote:
On Sat, 10 Jul 2004 19:21:23 GMT, Alex Hunsley
<la**@tardis.ed.ac.molar.uk> wrote:
I've using the csv module to parse a string, not a file.
So how do I make a string look like a file so I can call csv.reader()
with it?

An an example, I'd like to call something like:

csvReader = csv.reader("1,2,3,4")

.. but this direct attempt won't work as it expects a file or a handle...

what is cStringIO?


I'm not sure, but if you google there are answers:

http://www.google.com/search?hl=en&i...=Google+Search


sorry; I should stop giving answers as questions.

--
John Lenton (jl*****@gmail.com) -- Random fortune:
bash: fortune: command not found
Jul 18 '05 #6
John Lenton wrote:
On Sat, 10 Jul 2004 19:50:25 GMT, Alex Hunsley
<la**@tardis.ed.ac.molar.uk> wrote:
John Lenton wrote:

On Sat, 10 Jul 2004 19:21:23 GMT, Alex Hunsley
<la**@tardis.ed.ac.molar.uk> wrote:
I've using the csv module to parse a string, not a file.
So how do I make a string look like a file so I can call csv.reader()
with it?

An an example, I'd like to call something like:

csvReader = csv.reader("1,2,3,4")

.. but this direct attempt won't work as it expects a file or a handle...
what is cStringIO?


I'm not sure, but if you google there are answers:

http://www.google.com/search?hl=en&i...=Google+Search

sorry; I should stop giving answers as questions.


Oh, that was an answer! Sorry! I thought you were a newbie who had tried to
post a new topic but had just hit 'reply' or something like that...


Jul 18 '05 #7
On Saturday 10 July 2004 8:51 pm, Alex Hunsley wrote:
Foovoid Null wrote:
import StringIO

csvReader = csv.reader(StringIO.StringIO("1,2,3,4"))


Much thanks! That does the job nicely..
alex


I know this suggestion comes a bit late but

csvReader = csv.reader("1,2,3,4".splitlines(1))

may be a bit more efficient. csv.reader() does not actually require a
file-like object - any iterable will do, such as a list. The docstring is
explicit about this even though the online docs imply that the "csvfile"
object must have a next() method.

James
--
James Henderson, Logical Progression Ltd
http://www.logicalprogression.net
http://mailmanager.sourceforge.net

Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Knackeback | last post by:
task: - read/parse CSV file code snippet: string key,line; typedef tokenizer<char_separator<char> > tokenizer; tokenizer tok(string(""), sep); while ( getline(f, line) ){ ++lineNo;...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
3
by: SharpCoderMP | last post by:
i've run into some trouble using data from xml inside my app. the scenario is simple. input data looks more or less like this: <item> <name>MyName</name> <somefloat>11.5</somefloat> </item> ...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
11
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
2
by: zcabeli | last post by:
Hello everybody, i'd like to parse xml file which include records of the following type: each record has varied number of fields with either numerical or string value. it may also include...
6
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hello, I have an XML file generated from a third party application that I would like to parse. Ideally, I plan on having a windows service setup to scan various folders for XML files and parse the...
2
by: hsachdevah | last post by:
Hi, I developed an application for my study project to do some mathematical calculations. In this application, I am reading from a text file which contains some numbers. Now the problem is...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.