-1

I would like to ask how to change code to get output:

Nigel;UK;19
John;US;22
Carol;Germany;26

no this input (is it list?):

['Nigel', 'UK', '19']
['John', 'US', '22']
['Carol', 'Germany', '26']

code:

import csv

with open('friends.csv', 'r') as source:
    reader = csv.reader(source)

    for line in reader:
        print(line)

I think that now exist some instructions for it but I did not find it.
Thank you

Barmar
  • 741,623
  • 53
  • 500
  • 612
Mirek6
  • 25
  • 8
  • 3
    try use with open('friends.csv', 'r') as source: reader = source.readlines() for line in reader: print(line) – Rutangaba Oct 26 '21 at 20:44
  • Thank you, but it would be better without free lines between every line. – Mirek6 Oct 26 '21 at 20:49
  • And also need ; between words insted of , – Mirek6 Oct 26 '21 at 20:51
  • `print(';'.join(line))` – Mark Tolonen Oct 26 '21 at 20:52
  • so you can use `print(line.replace('\n', ''))` – Rutangaba Oct 26 '21 at 20:53
  • Does the first block show your input file or does it show your expected output? What is the separator in your input file? Commas or semicolons? – Pranav Hosangadi Oct 26 '21 at 20:55
  • @PranavHosangadi In my code it was at first mentioned output like `['Nigel', 'UK', '19']` but with your both answers I have right output, thank´s. – Mirek6 Oct 26 '21 at 20:58
  • The question is somewhat unclear and could be improved. For future reference, there's two ways to interpret the question: do you want the output printed to appear as a string, or do you want each line within an *actual* string variable? The interpretation is made ambiguous because you're not actually assigning each line to anything, you're just printing the result out in above example. – rv.kvetch Oct 26 '21 at 21:03

2 Answers2

0

Try unpacking the list elements via the * operator, and pass a custom separator to print:

import csv

with open('friends.csv', 'r') as source:
    reader = csv.reader(source)

    for line in reader:
        print(*line, sep=';')

Result:

Nigel;UK;19
John;US;22
Carol;Germany;26

An easier (and more efficient) approach would be to read in file contents as a string, and then replace all commas with a colon:

with open('friends.csv', 'r') as source:
    print(source.read().replace(',', ';'))

Note, this assumes the contents of your friends.csv is as follows:

Nigel,UK,19
John,US,22
Carol,Germany,26
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
0

You get the output as a list because that's what csv.reader() does! It reads each line of a csv file, converts it to a list taking into account commas and escaping rules, and returns that list.

If you don't care about each individual element of the csv file, just read the file as a regular file:

with open("filename.csv", "r") as f:
    for line in f:
        print(line, end="")

the end="" argument prevents print() from adding its own newline characters, since the line from the file already includes the newline character

Alternatively, you can still read it as a csv file, but str.join() the resulting list before printing it. Use this if you are using the rows of the csv file as a list somewhere else, but you just want to print the file here

with open('friends.csv', 'r') as source:
    reader = csv.reader(source)
    for line in reader:
        print(";".join(line))
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70