1

How to write data to csv file in Python? I have csv file with data which have to be transform.

I do it in this way:

import csv

with open('bookcat.csv') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
    if row:
        id = row[0].strip()
        categories = row[1:]
        for cat in categories:
            cat = cat.strip()
            if cat:
                print("%s\t%s" % (id, cat))

id and cat have to be write to csv file. Before executed it I have csv file which looks like:

    1,,,,,,,,201,202,,204,,206,207,208,,301,,,,,,,,,,,,,,,,,,,,,,,,605,606,,
    2,,,,,,,,201,,,204,205,,,,209,301,,,304,,402,,,405,,,,,,,,,,,,,,,604,,,,
    3,,,,,,,,201,,,,,,,,,,,,,,,,,,,,,,,506,,,,,,,,,,,,,
    4,,,,,,,,,,,204,,206,,208,209,,,,,,,,,,,,,,,,,,,,,,,,,,,,
    5,,,,,,,,201,,,204,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Now, after executed my code in Python it looks like:

    1   201
    1   202
    1   204
    1   206
    1   207
    1   208
    1   301
    1   605
    1   606
    2   201
    2   204
    2   205
    2   209
    2   301
    2   304

And i want to write it to csv file with delimiter ; or , .

spaceoddity11
  • 91
  • 1
  • 3
  • 10
  • Please don’t share code/data as images. Can you explain what the issue is? Have you read the csv docs? – AMC Jan 19 '20 at 16:54
  • hello, this image shosw how to data should looks like in the csv file which I have to create. – spaceoddity11 Jan 19 '20 at 17:15
  • Right, yes, I’m not sure how that relates to my comment though. – AMC Jan 19 '20 at 17:21
  • Ok, I edited my question. It is better now? My question is:how to save this data to a csv file? – spaceoddity11 Jan 19 '20 at 17:23
  • I was looking for something more specific. What have you tried? Which part are you struggling with? – AMC Jan 19 '20 at 17:24
  • Also, this question is most likely a duplicate of https://stackoverflow.com/q/37289951/11301900. – AMC Jan 19 '20 at 17:28
  • I added another information abuout my problem. I tried to do it like in the link from you but I failed,I dont know where and how to save data to a csv file with appriopriate delimiter. – spaceoddity11 Jan 19 '20 at 17:50
  • _I tried to do it like in the link from you but I failed,I dont know where and how to save data to a csv file with appriopriate delimiter._ **Please be more specific**. _with appriopriate delimiter_ That should be trivial, have you read the docs for the csv module? As an aside, why do you want to use a semicolon as the delimiter, instead of a comma? – AMC Jan 19 '20 at 18:09
  • Ok, delimiter is not important. Yes i read it and I can write one line to csv file but i cant write my transformed data in Python from csv file to another csv file. – spaceoddity11 Jan 19 '20 at 18:21
  • _I can write one line to csv file but i cant write my transformed data in Python from csv file to another csv file._ That isn't any more specific, detailed, or clear than your previous comment. – AMC Jan 19 '20 at 18:26
  • import csv with open('bookcat.csv') as file: writer = csv.writer(file) reader = csv.reader(file) for row in reader: if row: id = row[0].strip() writer.writerow(id) categories = row[1:] for cat in categories: cat = cat.strip() if cat: print("%s\t%s" % (id, cat)) writer.writerow(cat) – spaceoddity11 Jan 19 '20 at 18:30
  • I tried code like this but it didnt work – spaceoddity11 Jan 19 '20 at 18:31
  • What does _it didnt work_ mean, exactly? Don't share code or anything like that in the comments, it's difficult to read. – AMC Jan 19 '20 at 18:32
  • Where should I save the data after transforming them in the code so that in the csv file the row looks like this: id, cat? – spaceoddity11 Jan 19 '20 at 18:35
  • That's even more vague... Did you take a look at the other question I linked? – AMC Jan 19 '20 at 18:40
  • Yes i did it but in this question someone didnt transform data from csv. In my example i have csv file where each row contains a book identifier and several category identifiers and I want to change it so that each row has a book identifier and a category identifier. – spaceoddity11 Jan 19 '20 at 18:48
  • Your situation isn't exactly identical to that other question, but it doesn't matter, the way in which you write the output to a CSV file is the same. – AMC Jan 19 '20 at 18:50
  • yes, my problem is where and how to save the data to receive the file in the format that is right for me. If it were to write to the csv file without this data transformation then this would not be this question. – spaceoddity11 Jan 19 '20 at 18:54
  • Sorry if my comment wasn't clear, what I meant is that the "write to CSV" part of your program will follow the same general design as the solutions presented in that other question. At this point I would strongly recommend taking a step back, and trying to break down this entire problem/program on paper. – AMC Jan 19 '20 at 18:56

1 Answers1

1

Does this help?

import csv
with open('innovators.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["SN", "Name", "Contribution"])
    writer.writerow([1, "Linus Torvalds", "Linux Kernel"])
    writer.writerow([2, "Tim Berners-Lee", "World Wide Web"])
    writer.writerow([3, "Guido van Rossum", "Python Programming"])

Output

SN,Name,Contribution
1,Linus Torvalds,Linux Kernel
2,Tim Berners-Lee,World Wide Web
3,Guido van Rossum,Python Programming
Sahil
  • 1,387
  • 14
  • 41