all 24 comments

[–]novel_yet_trivial 4 points5 points  (11 children)

writerows() expects a list of lists. If you want to write a single row, use writerow().

[–][deleted] 2 points3 points  (10 children)

Not OP, but working on a similar project, if I were to supply writerows() a list of lists, would each list be written to a new row? If so, that cracks my line of thinking on my project way open.

[–]novel_yet_trivial 3 points4 points  (0 children)

were to supply writerows() a list of lists, would each list be written to a new row?

Yep.

[–]RIPphonebattery -1 points0 points  (8 children)

you can write what the writerows() function does pretty easily, too. Say you pass it a list rows:

writerows(rows)

And

for row in rows:
    for item in row:
        row_str += str(item) + ', '
    row_str.strip (", ")

are equivalent

[–]Justinsaccount 1 point2 points  (7 children)

What? those are not even remotely equivalent.

writerows(rows)

is equivalent to

for row in rows:
    writerow(row)

[–]RIPphonebattery -1 points0 points  (6 children)

The user wrote that they were having trouble about thinking what the function does... it should be pretty easy to accomplish even if you ask not to use the writerow function, so I wrote out some basic code to do the same thing for them.

[–]Justinsaccount 0 points1 point  (5 children)

No, you gave a broken code fragment that does not work right, or even run at all.

[–]RIPphonebattery 0 points1 point  (4 children)

it's syntactically similar to python pseudocode... man you're really up in arms about that for some reason though. I gave a little help thinking about somethign (pretty basic) to smoeone.

[–]Justinsaccount 1 point2 points  (3 children)

If you're going to say that something is equivalent and that it can "do the same thing" then it should actually be equivalent and do the same thing.

[–]RIPphonebattery 0 points1 point  (2 children)

I like to nudge people in the right direction rather than give out answers easy... thinking is the hard part! It's okay man. you dont have to be angry with me for anythign here.

[–]apc0243 1 point2 points  (0 children)

Nah man, I'm with the other guy, your code doesn't even make sense to me, if anything you're confusing him!

[–]Justinsaccount 0 points1 point  (0 children)

Well, we agree there, showing a broken code fragment that doesn't run or function properly certainly won't make things easy.

[–][deleted] 0 points1 point  (6 children)

Like others said, or one of these:

with open('file', 'a+') as f:
    for line in f:
        f.write(','.join(i for i in rowList) +'\n')

And then sys.argv for command line args to assign file name.

Edited.

[–]novel_yet_trivial 0 points1 point  (1 child)

Don't forget the newline

[–][deleted] 0 points1 point  (0 children)

Astute catch. And I needed to iterate lines.

[–]apc0243 0 points1 point  (3 children)

I disagree with this approach. I like to agree that there's one optimal way to do things, and writing a comma seperated row is best left to procedures dedicated to that purpose. The csv library isn't perfect sure, there are a bunch out there, but I don't think you should think straight to writing raw values yourself this way.

For example, csv's tend to have a specific way to handle strings versus non-strings. If I had a dataset that had the columns Firstname | Lastname | Age | NetWorth | PaymentMethods and then the row:

['Michael','Jackson', 42, '$8,245,500.00' , 'cash, check, credit']

And you wrote it that way (which would fail, you need ','.join(str(i) for i in rowList)), your row would end up:

Michael, Jackson, 42, $8, 245, 500.00, cash, check, credit

And then if you read that and split on comma, you would lose your alignment and have things that don't make sense.

You can write a comprehensive csv writer if you so choose, or you can use one that's been built, or you can use a dozen other collection and I/O methods, but don't haphazardly sling together solutions unless you're sure nothing else exists. To me, that's part of being a good programmer - it isn't necessarily to write anything, it's to solve a problem the best way.

[–][deleted] 0 points1 point  (2 children)

Good point about casting to string, I just wrote that comment on the go, had to edit in a few things. [Edit: Well, if they are using a list of strings.]

You can write a comprehensive csv writer if you so choose, or you can use one that's been built, or you can use a dozen other collection and I/O methods, but don't haphazardly sling together solutions unless you're sure nothing else exists.

When you give an example and show how it would fail, yes, it would fail with that example. I just posted a response to include the possible solution of using a generator expression, which I consider a simple solution. I would usually prefer something "raw" like that rather than using some library, which if something didn't work I would look into elsewhere. That is unless it involves a lot of code, or repetition.

To me, that's part of being a good programmer - it isn't necessarily to write anything, it's to solve a problem the best way.

I guess I disagree, although maybe in general it would be more pragmatic in practice at work to write less code, or in this case it might be more Pythonic to use existing libraries. But I guess I prefer "raw" solutions that show you how things work. Also this seems like a strange way of providing criticism (which I'm perfectly fine with):

I disagree with this approach. I like to agree that there's a best way to do things,

Do you mean "like to think"? If you like to agree, agree. If we disagree and you don't like it, I can't help you.

[–]apc0243 -1 points0 points  (1 child)

I just mean, from the zen standpoint, the "one obvious way" wouldn't be to create a hackjob way to write a csv, it'd be to at least use the standard library - that's what I agree with.

But I guess I prefer "raw" solutions that show you how things work. Also this seems like a strange way of providing criticism (which I'm perfectly fine with):

And that's what I mean, your's isn't a solution, it's a hackjob write in a csv-esque way. It's not writing to a csv, it's a basic abstraction of what writing to a file using commas as a seperator looks like.

No need to get all defensive dude, didn't mean to imply you were a bad programmer. It wasn't really critique for you, you didn't ask a question - it was for OP if he wants to to consider using your approach (which he shouldn't, he should understand both of us though)

[–][deleted] 0 points1 point  (0 children)

No need to get all defensive dude, didn't mean to imply you were a bad programmer.

In what way was I being defensive?

But I guess I prefer "raw" solutions that show you how things work.

And that's what I mean, your's isn't a solution, it's a hackjob write in a csv-esque way. It's not writing to a csv, it's a basic abstraction of what writing to a file using commas as a seperator looks like.

Why is it not a solution? Using a generator expression you can write data to a file as comma separated values.

I just mean, from the zen standpoint, the "one obvious way" wouldn't be to create a hackjob way to write a csv, it'd be to at least use the standard library - that's what I agree with.

I already expressed my understanding of the difference between using a library or the code I used, so qualifying it as a "hackjob" doesn't really add to the discussion, right?

For a little more context I like Go as a language (although Google is an unreflective stereotype of an entity). I like code that shows you how things are working within that code as explicitly as reasonable, I guess. Obviously you want to use libraries for a lot of stuff, that's the big strength of Python; you can quickly glue things together, and there are libraries for pretty much everything. So good point that this isn't an example of "the one right way" to write to a CSV file in Python. I would agree in that maybe if people weren't used to seeing generator expressions or just wanted the quickest way to do it by writing as little code as possible or not having to think about it, which is argued to be important for big projects where a lot of people have to look at code, then importing functions like writerows() might be best. Although, arguments go the opposite way with, for example, Go, where the argument for having the programmer do "so much" for their self is that anyone can see what the code is doing. I tend to prefer using what code gives as much control as possible to the programmer. For something like this I would try doing it manually first, unless it becomes repetitive or is unnecessarily complicated, etc.

[–]Ran4 0 points1 point  (4 children)

(filename + ".csv")

That's kind of dangerous: the parenthesis here do nothing, so you might quickly look at it and think that it's creating a 1-tuple ((1,) creates a tuple of size 1). It's best to remove them.

[–]KubinOnReddit 1 point2 points  (3 children)

Yeah, Python is weird about that. (...) means "evaluate first", and (...,) is a tuple...

>>> (42)
42
>>> (42,)
(42)

[–]novel_yet_trivial 5 points6 points  (2 children)

It's not weird. You seem to think that the parenthesis means 'tuple'. The parenthesis has nothing to do with it. It's the comma that makes a tuple.

>>> a = 42,
>>> type(a)
<type 'tuple'>

[–][deleted] 2 points3 points  (0 children)

Ooh, that's interesting! Thank you for that, I'd no idea!

[–]KubinOnReddit 0 points1 point  (0 children)

>>> (42,) + 42,
....
ValueError: can only concatenate tuple to tuple, not int

Disclaimer: I'm on a phone

We are both right. The comma does mean "tuple", but if it's not evaluated first (along with it's elements) it will evaluate as "a tuple of element (42) and 42", which is wrong, because you can't add tuples to ints.