Joel Divekar
Joel Divekar

Reputation: 187

Printing CSV data

Wanted to print csv data in single line.

CSVFile.txt:

abc|xyz|111
def|uvw|222

Python 3.x code:

with open(csvfile, "r") as csvdata:
    for line in csvdata:
        fields = line.split("|")
        for data in fields:
            # do some processing and then
            print(data)

This prints the data as

abc
xyz
111
def
uvw
222

Want to print data in straight line as

abc, xyz, 111
def, uvw, 222

Upvotes: 1

Views: 40

Answers (1)

Selcuk
Selcuk

Reputation: 59184

You can store your processed data in a list and print them together when you are finished processing each row instead:

...
row = []
for data in fields:
    # do some processing and then
    row.append(str(data))
print(", ".join(row))
...

Upvotes: 1

Related Questions