working with csv module in python

inshu chauhan insideshoes at gmail.com
Wed Feb 20 06:01:57 EST 2013


On Wed, Feb 20, 2013 at 11:38 AM, inshu chauhan <insideshoes at gmail.com>wrote:

>
>
>
> On Wed, Feb 20, 2013 at 11:26 AM, Roland Koebler <r.koebler at 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 "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 :

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

Traceback (most recent call last):
  File "C:\Users\inshu.chauhan\Desktop\Concatfiles.py", line 6, in <module>
    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 ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130220/0095ff78/attachment.html>


More information about the Python-list mailing list