Reading a csv file in python
Question:
I want to open and read file Mappe1.csv but it is not working. I am using Python 3.6.5 Anaconda. I do not know what I am doing wrong. Any ideas?
File Mappe1.csv contains:
A;B;C
1;4;7
2;5;8
3;6;9
The python code for opening Mappe1.csv looks like the following:
import csv
class CSVAdapter:
def read_csv(self):
with open("C:/Users/User/PycharmProjects/General/DesignUploader/Mappe1.csv") as file:
dialect = csv.Sniffer().sniff(file.read(1024))
csv_file = csv.reader(file.readlines(), dialect, quotechar='"')
print("A")
csv_file_list = []
for row in file:
print("B")
for row in csv_file:
print("C")
csv_file_list.append(row)
return csv_file_list
The printout is “A” only. “B” and “C” does not get printed which leads me to the conclusion that the file was not read correctly.
Are there any python/windows settings required to allow Python to read a file?
Thank you for your help!
Answers:
You need to supply to csv.reader
file object, not file.readlines()
. If you want to print Bs, you need to seek to the beginning of file, similar with the Cs.
import csv
def read_csv():
with open("data.csv") as file:
dialect = csv.Sniffer().sniff(file.read(1024))
csv_file = csv.reader(file, dialect, quotechar='"')
print("A")
csv_file_list = []
file.seek(0)
for row in file:
print("B")
file.seek(0)
for row in csv_file:
print("C")
csv_file_list.append(row)
return csv_file_list
print(read_csv())
Prints:
A
B
B
B
B
C
C
C
C
[['A', 'B', 'C'], ['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
In your code, you are only printing A
, B
, or C
. Try printing the whole line instead. You will see that the row
in cvs_file
is an object where we are only accessing one part of it. From the docs, see the example:
>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
... for row in spamreader:
... print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
From this example, try:
for row in csv_file:
print(','.join(row))
instead.
Finally, you will want to change the way the files are being read. Your cvs_files
is using delimiter
of ;
. Try that instead of the dialect
. And your file
does not actually have any property row
. You will need to
lines = file.readlines()
csv.reader takes a file object as first parameter, not a list which is what file.readlines() returns.
So your line:
csv_file = csv.reader(file.readlines(), dialect, quotechar='"')
should be:
csv_file = csv.reader(file, dialect, quotechar='"')
your for loop to print(‘B’) should be:
for row in file.readlines():
print('B')
your for loop to print(‘C’) should work fine like it is although it is worth noting that you could directly set csv_file_list = list(csv_file). And you should reset your file to the first line with:
file.seek(0)
This is alternative solution. I use the Pandas tool for reading CSV file when working with data. The code is actually very simple, and outputs the answer into a Pandas DataFrame
data structure. For me, tt is cleaner to read extracted information and easier to process than the csv tool.
Here is snippet:
import pandas
df = pandas.read_csv('C:/Users/User/PycharmProjects/General/DesignUploader/Mappe1.csv')
print (df)
I want to open and read file Mappe1.csv but it is not working. I am using Python 3.6.5 Anaconda. I do not know what I am doing wrong. Any ideas?
File Mappe1.csv contains:
A;B;C
1;4;7
2;5;8
3;6;9
The python code for opening Mappe1.csv looks like the following:
import csv
class CSVAdapter:
def read_csv(self):
with open("C:/Users/User/PycharmProjects/General/DesignUploader/Mappe1.csv") as file:
dialect = csv.Sniffer().sniff(file.read(1024))
csv_file = csv.reader(file.readlines(), dialect, quotechar='"')
print("A")
csv_file_list = []
for row in file:
print("B")
for row in csv_file:
print("C")
csv_file_list.append(row)
return csv_file_list
The printout is “A” only. “B” and “C” does not get printed which leads me to the conclusion that the file was not read correctly.
Are there any python/windows settings required to allow Python to read a file?
Thank you for your help!
You need to supply to csv.reader
file object, not file.readlines()
. If you want to print Bs, you need to seek to the beginning of file, similar with the Cs.
import csv
def read_csv():
with open("data.csv") as file:
dialect = csv.Sniffer().sniff(file.read(1024))
csv_file = csv.reader(file, dialect, quotechar='"')
print("A")
csv_file_list = []
file.seek(0)
for row in file:
print("B")
file.seek(0)
for row in csv_file:
print("C")
csv_file_list.append(row)
return csv_file_list
print(read_csv())
Prints:
A
B
B
B
B
C
C
C
C
[['A', 'B', 'C'], ['1', '4', '7'], ['2', '5', '8'], ['3', '6', '9']]
In your code, you are only printing A
, B
, or C
. Try printing the whole line instead. You will see that the row
in cvs_file
is an object where we are only accessing one part of it. From the docs, see the example:
>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
... spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
... for row in spamreader:
... print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam
From this example, try:
for row in csv_file:
print(','.join(row))
instead.
Finally, you will want to change the way the files are being read. Your cvs_files
is using delimiter
of ;
. Try that instead of the dialect
. And your file
does not actually have any property row
. You will need to
lines = file.readlines()
csv.reader takes a file object as first parameter, not a list which is what file.readlines() returns.
So your line:
csv_file = csv.reader(file.readlines(), dialect, quotechar='"')
should be:
csv_file = csv.reader(file, dialect, quotechar='"')
your for loop to print(‘B’) should be:
for row in file.readlines():
print('B')
your for loop to print(‘C’) should work fine like it is although it is worth noting that you could directly set csv_file_list = list(csv_file). And you should reset your file to the first line with:
file.seek(0)
This is alternative solution. I use the Pandas tool for reading CSV file when working with data. The code is actually very simple, and outputs the answer into a Pandas DataFrame
data structure. For me, tt is cleaner to read extracted information and easier to process than the csv tool.
Here is snippet:
import pandas
df = pandas.read_csv('C:/Users/User/PycharmProjects/General/DesignUploader/Mappe1.csv')
print (df)