w3resource

Python: Write (without writing separate lines between rows) and read a CSV file with specified delimiter

Python module: Exercise-6 with Solution

Write a Python program to write (without writing separate lines between rows) and read a CSV file with a specified delimiter. Use csv.reader

Sample Solution:

Python Code:

import csv     
fw = open("test.csv", "w", newline='')
writer = csv.writer(fw, delimiter = ",")
writer.writerow(["a","b","c"])
writer.writerow(["d","e","f"])
writer.writerow(["g","h","i"])
fw.close()
 
fr = open("test.csv", "r")
csv = csv.reader(fr, delimiter = ",")
for row in csv:
  print(row) 
fr.close()

Sample Output:

['a', 'b', 'c']
['d', 'e', 'f']
['g', 'h', 'i']

Flowchart:

Flowchart: Write (without writing separate lines between rows) and read a CSV file with specified delimiter.

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to skip the headers of a given CSV file.
Next: Write a Python program to write dictionaries and a list of dictionaries to a given CSV file.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Inverts a dictionary with non-unique hashable values:

Example:

def tips_collect_dictionary(obj):
  inv_obj = {}
  for key, value in obj.items():
    inv_obj.setdefault(value, list()).append(key)
  return inv_obj
ages = {
  "Owen": 25,
  "Jhon": 25,
  "Pepe": 15,
}
print(tips_collect_dictionary(ages))

Output:

{25: ['Owen', 'Jhon'], 15: ['Pepe']}

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook