try to split the row of a csv output - python

Now I am writing some data into a csv file. I directly write a list to a row of a csv file, like below:
with open("files/data.csv", "wb") as f_csv:
writer = csv.writer(f_csv,delimiter = ',')
writer.writerow(flux_inteplt) ## here flux_inteplt is a list
But when I read the data like below:
with open('files/data.csv','rb') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for row in reader:
parts = row.split(",")
print parts[0]
It has some problem AttributeError: 'list' object has no attribute 'split'
Does anyone has some idea how to approach to this problem?

import csv
with open('us-cities.csv','rb') as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for row in reader:
str1 = ''.join(row) #Convert list into string
parts = str1.split(",")
print parts[0]

row is already a list, when you iterate over the reader object you get a list of values split by the delimiter you pass, just use each row:
for row in reader:
print row[0] # first element from each row
If you have comma separated values use delimiter=',' not delimiter=' ', which based on the fact you use csv.writer(f_csv,delimiter = ',') when writing means you have. The delimiter you pass when writing is what is used to delimit each element from your input iterable so when reading you need to use the same delimiter if you want to get the same output.

row is already a list. No need to split (:

Related

Count commas in a csv file using python

I'm trying to count the commas row by row in a .csv file. Unfortunately it always comes up to zero.
import csv
with open('Test.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
numCommas = row.read().count(',')
print numCommas
But I am always getting 0.
If you just want to count the commas and don't need the data, the csv module is not required:
with open('Test.csv', 'r') as csv_file:
for line in csv_file:
print(line.count(','))
On python2 you can try in this way, but you will have to change the delimiter:
csv_reader = csv.reader(csv_file, delimiter='\t')
numCommas = row[0].count(',')
If you have the delimiter as , this is how the row looks like:
['a', 'd', 'f', 'g', 'h']
With \t as delimiter, the row is in this way:
['a,d,f,g,h']
So in this way you can have the number of commas for each row and not the total count
Just read the file and count ','
with open('Test.csv') as csv_file:
count = csv_file.read().count(',')
Code you have shared will give error, because in your for loop row will be list. And list object don't have attribute 'read'.
And you have used csv.reader, so it will give you each row in form of a list. So when you are iterating csv_reader object in for loop row variable will be of type list.
If you want to count number of columns in each row you can simply print len(row) inside for loop.
print len(row)
But if you want to count number of commas only, then you need to read file without using csv.reader.
From your example above the row in csv_reader is a list. It's not a string separated by comma. When you read the file through csv.reader() you breaking the rows down to each columns and storing them into a list and then it's put inside csv reader object.
For your purposes, may be you can simply use len(row) if you want the count of columns or items in the row.
It comes up with zero because the csv file is separated with the delimiter and the "row" variable is a list object.
You can get the count of comma(s) in str object like this:
a = "a,b,b,"
print(a.count(","))
This cannot be applied for the list, so you need to apply count for individual entries in your "row" variable to get the count of commas(if you need count for each entry in row) or you can read the file as a text file. and get the commas by readlines method.
csv_reader = csv.reader(csv_file, delimiter=',')
This line separates the elements by ',' and return a list. For instance, a csv row could be
100, 200, 300, 400
The list returned would be [100, 200, 300, 400]
To count the comma, subtract 1 from the number of elements. i.e. 4 elements = 3 commas
FIXED VERSION:
with open('Test.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
print (len(row)-1)

How to find if any element within a list is present within a row in a CSV file when using a for loop

import csv
with open('example.csv', 'r') as f:
csvfile = csv.reader(f, delimiter = ',')
client_email = ['#example.co.uk', '#moreexamples.com', 'lastexample.com']
for row in csvfile:
if row not in client_email:
print row
Assume code is formatted in blocks properly, it's not translating properly when I copy paste. I've created a list of company email domain names (as seen in the example), and I've created a loop to print out every row in my CSV that is not present in the list. Other columns in the CSV file include first name, second name, company name etc. so it is not limited to only emails.
Problem is when Im testing, it is printing off rows with the emails in the list i.e jackson#example.co.uk.
Any ideas?
In your example, row refers to a list of strings. So each row is ['First name', 'Second name', 'Company Name'] etc.
You're currently checking whether any column is exactly one of the elements in your client_email.
I suspect you want to check whether the text of any column contains one of the elements in client_email.
You could use another loop:
for row in csvfile:
for column in row:
# check if the column contains any of the email domains here
# if it does:
print row
continue
To check if a string contains any strings in another list, I often find this approach useful:
s = "xxabcxx"
stop_list = ["abc", "def", "ghi"]
if any(elem in s for elem in stop_list):
pass
One way to check may be to see if set of client_email and set in row has common elements (by changing if condition in loop):
import csv
with open('example.csv', 'r') as f:
csvfile = csv.reader(f, delimiter = ',')
client_email = ['#example.co.uk', '#moreexamples.com', 'lastexample.com']
for row in csvfile:
if (set(row) & set(client_email)):
print (row)
You can also use any as following:
import csv
with open('untitled.csv', 'r') as f:
csvfile = csv.reader(f, delimiter = ',')
client_email = ['#example.co.uk', '#moreexamples.com', 'lastexample.com']
for row in csvfile:
if any(item in row for item in client_email):
print (row)
Another possible way,
import csv
data = csv.reader(open('example.csv', 'r'))
emails = {'#example.co.uk', '#moreexamples.com', 'lastexample.com'}
for row in data:
if any(email in cell for cell in row for email in emails):
print(row)

How to read one single line of csv data in Python?

There is a lot of examples of reading csv data using python, like this one:
import csv
with open('some.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
I only want to read one line of data and enter it into various variables. How do I do that? I've looked everywhere for a working example.
My code only retrieves the value for i, and none of the other values
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in reader:
i = int(row[0])
a1 = int(row[1])
b1 = int(row[2])
c1 = int(row[2])
x1 = int(row[2])
y1 = int(row[2])
z1 = int(row[2])
To read only the first row of the csv file use next() on the reader object.
with open('some.csv', newline='') as f:
reader = csv.reader(f)
row1 = next(reader) # gets the first line
# now do something here
# if first row is the header, then you can do one more next() to get the next row:
# row2 = next(f)
or :
with open('some.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
# do something here with `row`
break
you could get just the first row like:
with open('some.csv', newline='') as f:
csv_reader = csv.reader(f)
csv_headings = next(csv_reader)
first_line = next(csv_reader)
You can use Pandas library to read the first few lines from the huge dataset.
import pandas as pd
data = pd.read_csv("names.csv", nrows=1)
You can mention the number of lines to be read in the nrows parameter.
Just for reference, a for loop can be used after getting the first row to get the rest of the file:
with open('file.csv', newline='') as f:
reader = csv.reader(f)
row1 = next(reader) # gets the first line
for row in reader:
print(row) # prints rows 2 and onward
From the Python documentation:
And while the module doesn’t directly support parsing strings, it can easily be done:
import csv
for row in csv.reader(['one,two,three']):
print row
Just drop your string data into a singleton list.
The simple way to get any row in csv file
import csv
csvfile = open('some.csv','rb')
csvFileArray = []
for row in csv.reader(csvfile, delimiter = '.'):
csvFileArray.append(row)
print(csvFileArray[0])
To print a range of line, in this case from line 4 to 7
import csv
with open('california_housing_test.csv') as csv_file:
data = csv.reader(csv_file)
for row in list(data)[4:7]:
print(row)
I think the simplest way is the best way, and in this case (and in most others) is one without using external libraries (pandas) or modules (csv). So, here is the simple answer.
""" no need to give any mode, keep it simple """
with open('some.csv') as f:
""" store in a variable to be used later """
my_line = f.nextline()
""" do what you like with 'my_line' now """

Python to insert quotes to column in CSV

I have no knowledge of python.
What i want to be able to do is create a script that will edit a CSV file so that it will wrap every field in column 3 around quotes. I haven't been able to find much help, is this quick and easy to do? Thanks.
column1,column2,column3
1111111,2222222,333333
This is a fairly crude solution, very specific to your request (assuming your source file is called "csvfile.csv" and is in C:\Temp).
import csv
newrow = []
csvFileRead = open('c:/temp/csvfile.csv', 'rb')
csvFileNew = open('c:/temp/csvfilenew.csv', 'wb')
# Open the CSV
csvReader = csv.reader(csvFileRead, delimiter = ',')
# Append the rows to variable newrow
for row in csvReader:
newrow.append(row)
# Add quotes around the third list item
for row in newrow:
row[2] = "'"+str(row[2])+"'"
csvFileRead.close()
# Create a new CSV file
csvWriter = csv.writer(csvFileNew, delimiter = ',')
# Append the csv with rows from newrow variable
for row in newrow:
csvWriter.writerow(row)
csvFileNew.close()
There are MUCH more elegant ways of doing what you want, but I've tried to break it down into basic chunks to show how each bit works.
I would start by looking at the csv module.
import csv
filename = 'file.csv'
with open(filename, 'wb') as f:
reader = csv.reader(f)
for row in reader:
row[2] = "'%s'" % row[2]
And then write it back in the csv file.

How to avoid splitting on a delimiter if it appears inside quotes?

I have problem in splitting data. I have data as follows in CSV file:
"a";"b";"c;d";"e"
The problem is when I used line.split(";") function, it splits even between c and d. I don't want c and d to be separated. Later I need to store these four values in four different columns in a table, but using this function I get five different columns.
I want the results to be "a" "b" "cd" "e".
I tried with line.split('";"'), but it did not help.
import csv
reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';')
for row in reader:
print row
Try this out.
import csv
reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';', quoting=csv.QUOTE_NONE )
for row in reader:
print row
This ^^^ if you want quotes preserved
Edit: If you want ';' removed from the field content ('c;d' = 'cd' case) - you may do the post processing on rows returned, something like this:
import csv
reader = csv.reader(open("yourfile.csv", "rb"), delimiter=';', quoting=csv.QUOTE_NONE )
for row in reader:
print [item.replace(';', '') for item in row]
In other contexts, the shlex.split() function could be used

Categories