Reading with xlrd in python - python

I wrote this program to read a column from an excel file then write it into a txt file:
import xlrd, sys
text_file = open("Output.txt", "w")
isotope = xlrd.open_workbook(sys.argv[1])
first_sheet=isotope.sheet_by_index(0)
x= []
for rownum in range(first_sheet.nrows):
x.append(first_sheet.cell(rownum, 1))
for item in x:
text_file.write("%s\n" % item)
text_file.close()
It reads the column correctly but writes it like so:
number:517.0
number:531.0
number:517.0
number:520.0
number:513.0
number:514.0
number:522.0
Can I read it in a way that it just writes the value and not "number:"? I could just cut out the first 7 characters of every line, but that seems kind of inefficient.
Thanks for the help!

Also, if you want a way to read entire values of a row in one shot:
You can take first_sheet and do:
first_sheet.row_values(index_of_row)
This will return a list with all the values of the index_of_row.

Related

Adding new row in a CSV Python

I am adding a new row to a specific CSV that already exist, but for unknown reason the new row is being added along with the last row and not in a new one.
So, it's showing in CSV as:
11-07-2016,38361,9076,14487,292,741614-07-2016,38417,9767,15832,301,7416
When should be showing as:
11-07-2016,38361,9076,14487,292,7416
14-07-2016,38417,9767,15832,301,7416
My code is:
import time
import csv
today = (time.strftime("%d-%m-%Y"))
newRow = """%s,%s,%s,%s,%s,%s""" % (today, yes, ok, war, leg, noag)
fd = open('data.csv','a')
fd.write(newRow)
fd.close()
Any idea why this is happening? Thanks a lot.
It looks like the file doesn't have a newline at the end. Try adding one before appending the new line:
newRow = "\n%s,%s,%s,%s,%s,%s\n" % (today, yes, ok, war, leg, noag)
with open("data.csv", "a") as f:
f.write(newRow)
Right now you are not using the csv module, just the regular write as for text file.
To treat the file as a csv change:
fd.write(newRow)
to:
csv_writer = csv.writer(fd)
csv_writer.writerow(newRow)
If you want to edit the file as a text file you should add the "new line charecter" manualy, so:
fd.write('\n' + newRow)
will work (in that case the import csv is redundant)
import time
import csv
today = (time.strftime("%d-%m-%Y"))
newRow = """%s,%s,%s,%s,%s,%s""" % (today, yes, ok, war, leg, noag)
with open('data.csv','a',newline='') as fd:
writer = csv.writer(fd)
writer.writerow(newRow)
It's writerow instead of write, it'll automatically add a new line. and newline = '' prevents your skip a line.

re.sub for a csv file

I am receiving a error on this code. It is "TypeError: expected string or buffer". I looked around, and found out that the error is because I am passing re.sub a list, and it does not take lists. However, I wasn't able to figure out how to change my line from the csv file into something that it would read.
I am trying to change all the periods in a csv file into commas. Here is my code:
import csv
import re
in_file = open("/test.csv", "rb")
reader = csv.reader(in_file)
out_file = open("/out.csv", "wb")
writer = csv.writer(out_file)
for row in reader:
newrow = re.sub(r"(\.)+", ",", row)
writer.writerow(newrow)
in_file.close()
out_file.close()
I'm sorry if this has already been answered somewhere. There was certainly a lot of answers regarding this error, but I couldn't make any of them work with my csv file. Also, as a side note, this was originally an .xslb excel file that I converted into csv in order to be able to work with it. Was that necessary?
You could use list comprehension to apply your substitution to each item in row
for row in reader:
newrow = [re.sub(r"(\.)+", ",", item) for item in row]
writer.writerow(newrow)
for row in reader does not return single element to parse it rather it returns list of of elements in that row so you have to unpack that list and parse each item individually, just like #Trii shew you:
[re.sub(r'(\.)+','.',s) for s in row]
In this case, we are using glob to access all the csv files in the directory.
The code below overwrites the source csv file, so there is no need to create an output file.
NOTE:
If you want to get a second file with the parameters provided with re.sub, replace write = open(i, 'w') for write = open('secondFile.csv', 'w')
import re
import glob
for i in glob.glob("*.csv"):
read = open(i, 'r')
reader = read.read()
csvRe = re.sub(re.sub(r"(\.)+", ",", str(reader))
write = open(i, 'w')
write.write(csvRe)
read.close()
write.close()

csv row and column fetch

So working on a program in Python 3.3.2. New to it all, but I've been getting through it. I have an app that I made that will take 5 inputs. 3 of those inputs are comboboxs, two are entry widgets. I have then created a button event that will save those 5 inputs into a text file, and a csv file. Opening each file everything looks proper. For example saved info would look like this:
Brad M.,Mike K.,Danny,Iconnoshper,Strong Wolf Lodge
I then followed a csv demo and copied this...
import csv
ifile = open('myTestfile.csv', "r")
reader = csv.reader(ifile)
rownum = 0
for row in reader:
# Save header row.
if rownum == 0:
header = row
else:
colnum = 0
for col in row:
print('%-15s: %s' % (header[colnum], col))
colnum += 1
rownum += 1
ifile.close()
and that ends up printing beautifully as:
rTech: Brad M.
pTech: Mike K.
cTech: Danny
proNam: ohhh
jobNam: Yeah
rTech: Damien
pTech: Aaron
so on and so on. What I'm trying to figure out is if I've named my headers via
if rownum == 0:
header = row
is there a way to pull a specific row / col combo and print what is held there??
I have figured out that I could after the program ran do
print(col)
or
print(col[0:10]
and I am able to print the last col printed, or the letters from the last printed col. But I can't go any farther back than that last printed col.
My ultimate goal is to be able to assign variables so I could in turn have a label in another program get it's information from the csv file.
rTech for job is???
look in Jobs csv at row 1, column 1, and return value for rTech
do I need to create a dictionary that is loaded with the information then call the dictionary?? Thanks for any guidance
Thanks for the direction. So been trying a few different things one of which Im really liking is the following...
import csv
labels = ['rTech', 'pTech', 'cTech', 'productionName', 'jobName']
fn = 'my file.csv'
cameraTech = 'Danny'
f = open(fn, 'r')
reader = csv.DictReader(f, labels)
jobInformation = [(item["productionName"],
item["jobName"],
item["pTech"],
item["rTech"]) for item in reader if \
item['cTech'] == cameraTech]
f.close()
print ("Camera Tech: %s\n" % (cameraTech))
print ("\n".join(["Production Name: %s \nJob Name: %s \nPrep Tech: %s \nRental Agent: %s\n" % (item) for item in jobInformation]))
That shows me that I could create a variable through cameraTech and as long as that matched what was loaded into the reader that holds the csv file and that if cTech column had a match for cameraTech then it would fill in the proper information. 95% there WOOOOOO..
So now what I'm curious about is calling each item. The plan is in a window I have a listbox that is populated with items from a .txt file with "productionName" and "jobName". When I click on one of those items in the listbox a new window opens up and the matching information from the .csv file is then filled into the appropriate labels.
Thoughts??? Thanks again :)
I think that reading the CSV file into a dictionary might be a working solution for your problem.
The Python CSV package has built-in support for reading CSV files into a Python dictionary using DictReader, have a look at the documentation here: http://docs.python.org/2/library/csv.html#csv.DictReader
Here is an (untested) example using DictReader that reads the CSV file into a Python dictionary and prints the contents of the first row:
import csv
csv_data = csv.DictReader(open("myTestfile.csv"))
print(csv_data[0])
Okay so I was able to put this together after seeing the following (https://gist.github.com/zstumgoren/911615)
That showed me how to give each header a variable I could call. From there I could then create a function that would allow for certain variables to be called and compared and if that matched I would be able to see certain data needed. So the example I made to show myself it could be done is as follows:
import csv
source_file = open('jobList.csv', 'r')
for line in csv.DictReader(source_file, delimiter=','):
pTech= line['pTech']
cTech= line['cTech']
rAgent= line['rTech']
prodName= line['productionName']
jobName= line['jobName']
if prodName == 'another':
print(pTech, cTech, rAgent, jobName)
However I just noticed something, while my .csv file has one line this works great!!!! But, creating my proper .csv file, I am only able to print information from the last line read. Grrrrr.... Getting closer though.... I'm still searching but if someone understands my issue, would love some light.

Python csv reader returns formula instead of value

I have a txt file which has some 'excel formulas', I have converted this to a csv file using Python csv reader/writer. Now I want to read the values of the csv file and do some calculation, but when i try to access the particular column of .csv file, it still returns me in the 'excel formula' instead of the actual value?? although When i open the csv file .. formulas are converted in to value??
Any ideas?
Here is the code
Code to convert txt to csv
def parseFile(filepath):
file = open(filepath,'r')
content = file.read()
file.close()
lines = content.split('\n')
csv_filepath = filepath[:(len(filepath)-4)]+'_Results.csv'
csv_out = csv.writer(open(csv_filepath, 'a'), delimiter=',' , lineterminator='\n')
for line in lines:
data = line.split('\t')
csv_out.writerow(data)
return csv_filepath
Code to do some calculation in csv file
def csv_cal (csv_filepath):
r = csv.reader(open(csv_filepath))
lines = [l for l in r]
counter =[0]*(len(lines[4])+6)
if lines[4][4] == 'Last Test Pass?' :
print ' i am here'
for i in range(0,3):
print lines[6] [4] ### RETURNS FORMULA ??
return 0
I am new to python, any help would be appreciated!
Thanks,
You can paste special in Excel with Values only option selected. You could select all and paste into a another sheet and save. This would save you from having to implement some kind of parser in python. Or, you could evaluate some simple arithmetic with eval.
edit:
I've heard of xlrd which can be downloaded from pypi. It loads .xls files.
It sounded like you just wanted the final data which past special can do.

Writing to a CSV with python without brackets

I am trying to write the below information to a csv file in this format:
Tmin, -40
However my output looks like the below with the brackets and quotes around the -40:
Tmin, ['-40']
My code is below. Basically I am searching through a csv file for a specific phrase, in this case "Tmin", and reading the entry 1 cell to the right. I then want to write both "Tmin" and the value read directly to a second csv file.
I have searched for a way to remove the quotes and brackets but have not been able to get it to work. I am pretty new to python so any help would be helpful. Thanks.
import csv
fileInput = "Input.csv"
fileOutput = "output.csv"
Name_Column = 0
Value_Column = 1
with open(fileInput, 'r') as file:
reader = csv.reader(file)
Tmin = [line[Value_Column] for line in reader if line[Name_Column] == 'Tmin']
print "Tmin=", Tmin #print to screen to check value read
with open (fileOutput,'w') as file:
writer = csv.writer(file)
print writer.writerow(['Tmin',Tmin])
python is just printing the Tmin as a list, you can either print the CSV row as a string, or just the first element in the row.
Try
print "Tmin=", ", ".join(Tmin)
or
print "Tmin=", Tmin[0]
if you want to have csv output through the csv writer, you need a single list, not a list inside a list:
Try
writer.writerow(['Tmin']+Tmin) #note this is already printing the result to a file so dont "print" the return value

Categories

Resources