Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.05; 'column': 0.07; 'skip:o 50': 0.07; 'python': 0.09; '"w")': 0.09; 'delimited': 0.09; 'delimiter': 0.09; 'read()': 0.09; 'rows': 0.09; 'skip:o 60': 0.09; 'subject:module': 0.09; 'windows,': 0.09; 'stored': 0.10; 'subject:python': 0.11; 'files.': 0.13; 'resulting': 0.13; "'w'": 0.16; 'columns': 0.16; 'concat': 0.16; 'csv': 0.16; 'f.read()': 0.16; 'prog': 0.16; 'row': 0.16; 'skip:g 50': 0.16; 'x,y,z': 0.16; '\xa0\xa0\xa0\xa0\xa0\xa0': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'skip:g 40': 0.17; 'feb': 0.19; 'module': 0.19; 'code.': 0.20; 'email addr:gmail.com>': 0.20; 'skip:" 40': 0.20; 'to:name:python-list@python.org': 0.20; 'file.': 0.20; 'trying': 0.21; 'import': 0.21; "skip:' 40": 0.22; '>': 0.23; 'so.': 0.24; 'linux': 0.24; 'tried': 0.25; 'header :In-Reply-To:1': 0.25; '(most': 0.27; 'am,': 0.27; 'merge': 0.27; 'module.': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; '+0100,': 0.29; 'them?': 0.29; 'skip:& 10': 0.29; 'source': 0.29; 'folder': 0.30; 'mode': 0.30; 'error': 0.30; 'code': 0.31; 'file': 0.32; 'print': 0.32; 'null': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'skip:& 20': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'so,': 0.35; 'open': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'subject:with': 0.36; 'should': 0.36; 'possible': 0.37; 'why': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'object': 0.38; 'some': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'space': 0.39; 'skip:" 10': 0.40; 'think': 0.40; 'your': 0.60; 'first': 0.61; 'between': 0.63; 'dont': 0.64; 'here': 0.65; '20,': 0.65; 'skip:c 50': 0.66; 'obvious': 0.71; '2013': 0.84; '299': 0.84; 'contents,': 0.84; 'writing,': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=fUOIHiAp7cxzy87MQpn3vcpnxOdyiZTmIxeJra7F4Dc=; b=qHMhuflREnmsacQB8+9aHqhd45r+5mr63oZH0WLpA/TGIJTWhbez9RH+EjGHNeWeAG jgolHXf+Es/weaOl7PdwLnels/b6K6+SpRA8EbtDjuqE3M56P6nasyK5A8snv/d2SdfF uoB0N0fYDZkxUaaULdiAnD15J3peHvBzECaWAbJEO9t/ec/wkBeB/mVFrn2PkDDGLCYZ cJAEhoV7HgOUllYAFmQVLOShEX1d85ruNblPI86MC+KFqd1/rGD9AjvAzMh5TD/XDZiM RoKdHIKsoW2SdlhVmYkzIgG5QtECtRoHW0tAvUZweGH666p/0H5KkHcKGyjmBtwUuXxO wf1g== MIME-Version: 1.0 X-Received: by 10.50.182.234 with SMTP id eh10mr9992792igc.40.1361358118063; Wed, 20 Feb 2013 03:01:58 -0800 (PST) In-Reply-To: References: <20130220102601.GD22457@localhost> Date: Wed, 20 Feb 2013 12:01:57 +0100 Subject: Re: working with csv module in python From: inshu chauhan To: "python-list@python.org" Content-Type: multipart/alternative; boundary=14dae9340ae90a0d3304d625e3e7 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 163 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361358127 news.xs4all.nl 6891 [2001:888:2000:d::a6]:39482 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39343 --14dae9340ae90a0d3304d625e3e7 Content-Type: text/plain; charset=ISO-8859-1 On Wed, Feb 20, 2013 at 11:38 AM, inshu chauhan wrote: > > > > On Wed, Feb 20, 2013 at 11:26 AM, Roland Koebler wrote: > >> Hi, >> >> On Wed, Feb 20, 2013 at 10:50:54AM +0100, inshu chauhan wrote: >> > I have 10 simple text files with 3 columns x,y,z delimited by "space". >> I am >> > trying to combine these 10 files to get a single text file. >> > >> > Eg. of data in 10 files is >> > 299 446 2 >> Do you only want to concat the files, or do you want to parse/mangle >> them? >> >> If you only want to concat the files, I would use some shell-tools, >> like "cat" on Linux or "copy" on Windows, so >> >> copy C:\Users\inshu.chauhan\Desktop\ForModel_600\*.arff >> C:\Users\inshu.chauhan\Desktop\test2.arff >> >> should do it. >> >> > Can in some some way I set delimiter to NULL as the prog gives me error >> if >> > I do so. >> Of course -- a CSV without a delimiter doesn't make any sense. >> >> > I dont why there is space between the attribute of first column in >> > reading and there is space between every row too.. >> Because there's a "split()" missing in your code. You currently tell the >> CSV-writer to write the columns 2,9,9, , , ,4,4,6, , , ,2 as >> space-separated CSV. So, try something like >> rows = [r.split() for r in open(f, "r").readlines()] >> >> > Or can I merge these text files without using csv module , directly in >> > python ? >> If you don't need to parse/mangle the contents, you don't need the csv >> module. Simple open the resulting file for writing, and then read out >> the source files and write their contents into the resulting file. >> >> >> Yes I just want to concat the files , not parse/mangle the files. How > can i simply read all files in a folder in my computer and write them into > a single file ? just by 'printf ' is it possible ? > > > For simple concating the files , I tried the following code : import glob with open(r"C:\Users\inshu.chauhan\Desktop\test2.arff", "w") as w: print w for f in glob.glob(r"C:\Users\inshu.chauhan\Desktop\For Model_600\*.arff"): g = f.read() w.write(g) But I think I am having an obvious error : Traceback (most recent call last): File "C:\Users\inshu.chauhan\Desktop\Concatfiles.py", line 6, in g = f.read() AttributeError: 'str' object has no attribute 'read' Here I am trying to refer the files stored in my folder by 'f', Why read() is not working ? --14dae9340ae90a0d3304d625e3e7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Wed, Feb 20, 2013 at 11:38 AM, inshu chauhan &= lt;insideshoes@g= mail.com> wrote:



On Wed, Feb 20, 2013 at 11:26 AM, Roland Koebler = <r.koebler@yahoo= .de> wrote:
Hi,

On Wed, Feb 20, 2013 at 10:50:54AM +0100, inshu chauhan wrote:
> I have 10 simple text files with 3 columns x,y,z delimited by "sp= ace". I am
> trying to combine these 10 files to get a single text file.
>
> Eg. of data in 10 files is
> 299 446 2
Do you only want to concat the files, or do you want to parse/mangle<= br> them?

If you only want to concat the files, I would use some shell-tools,
like "cat" on Linux or "copy" on Windows, so

copy C:\Users\inshu.chauhan\Desktop\ForModel_600\*.arff C:\Users\inshu.chau= han\Desktop\test2.arff

should do it.

> Can in some some way I set delimiter to NULL as the prog gives me erro= r if
> I do so.
Of course -- a CSV without a delimiter doesn't make any sense.

> I dont why there is space between the attribute of first column in
> reading and there is space between every row too..
Because there's a "split()" missing in your code. You c= urrently tell the
CSV-writer to write the columns 2,9,9, , , ,4,4,6, , , ,2 as
space-separated CSV. So, try something like
rows =3D [r.split() for r in open(f, "r").readlines()]

> Or can I merge these text files without using csv module , directly in=
> python ?
If you don't need to parse/mangle the contents, you don't nee= d the csv
module. Simple open the resulting file for writing, and then read out
the source files and write their contents into the resulting file.


Yes I just want to concat the files , not parse/mangle the files.=A0 How= can i simply read all files in a folder in my computer and write them into= a single file ? just by 'printf ' is it possible ?


For simple concating the = files , I tried the following code :

=A0import glob
<= br>with open(r"C:\Users\inshu.chauhan\Desktop\test2.arff", "= w") as w:
=A0=A0=A0 print w
=A0=A0=A0 for f in glob.glob(r"C:\Users\inshu.ch= auhan\Desktop\For Model_600\*.arff"):
=A0=A0=A0=A0=A0=A0=A0 g =3D f= .read()
=A0=A0=A0=A0=A0=A0=A0 w.write(g)
=A0=A0=A0=A0=A0=A0 =A0
= =A0=A0=A0=A0=A0=A0
But I think I am having an obvious error = :

<open file 'C:\Users\inshu.chauhan\Desktop\test2.arff', mode= 'w' at 0x01B64F40>

Traceback (most recent call last):=A0 File "C:\Users\inshu.chauhan\Desktop\Concatfiles.py", line 6= , in <module>
=A0=A0=A0 g =3D f.read()
AttributeError: 'str' object has no att= ribute 'read'

Here I am trying to refer the files= stored in my folder by 'f',=A0 Why read() is not working ?

--14dae9340ae90a0d3304d625e3e7--