1

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
2
  • What is fields? Is it a single row or the whole file? Commented Nov 6, 2019 at 6:00
  • updated my question with more code Commented Nov 6, 2019 at 6:12

1 Answer 1

1

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))
...
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.