Writing to a CSV with python without brackets - python

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

Related

All of my data from columns of one file go into one column in my output file. How to keep it the same?

I'm trying to delete some number of data rows from a file, essentially just because there are too many data points. I can easily print them to IDLE but when I try to write the lines to a file, all of the data from one row goes into one column. I'm definitely a noob but it seems like this should be "trivial"
I've tried it with writerow and writerows, zip(), with and without [], I've changed the delimiter and line terminator.
import csv
filename = "velocity_result.csv"
with open(filename, "r") as source:
for i, line in enumerate(source):
if i % 2 == 0:
with open ("result.csv", "ab") as result:
result_writer = csv.writer(result, quoting=csv.QUOTE_ALL, delimiter=',', lineterminator='\n')
result_writer.writerow([line])
This is what happens:
input = |a|b|c|d| <row
|e|f|g|h|
output = |abcd|
<every other row deleted
(just one column)
My expectaion is
input = |a|b|c|d| <row
|e|f|g|h|
output = |a|b|c|d|
<every other row deleted
Once you've read the line, it becomes a single item as far as Python is concerned. Sure, maybe it is a string which has comma separated values in it, but it is a single item still. So [line] is a list of 1 item, no matter how it is formatted.\
If you want to make sure the line is recognized as a list of separate values, you need to make it such, perhaps with split:
result_writer.writerow(line.split('<input file delimiter here>'))
Now the line becomes a list of 4 items, so it makes sense for csv writer to write them as 4 separated values in the file.

Parsing a text file with line breaks in python

I have a text file with about 20 entries. They look like this:
~
England
Link: http://imgur.com/foobar.jpg
Capital: London
~
Iceland
Link: http://imgur.com/foobar2.jpg
Capital: Reykjavik
...
etc.
I would like to take these entries and turn them into a CSV.
There is a '~' separating each entry. I'm scratching my head trying to figure out how to go thru line by line and create the CSV values for each country. Can anyone give me a clue on how to go about this?
Use the libraries luke :)
I'm assuming your data is well formatted. Most real world data isn't that way. So, here goes a solution.
>>> content.split('~')
['\nEngland\nLink: http://imgur.com/foobar.jpg\nCapital: London\n', '\nIceland\nLink: http://imgur.com/foobar2.jpg\nCapital: Reykjavik\n', '\nEngland\nLink: http://imgur.com/foobar.jpg\nCapital: London\n', '\nIceland\nLink: http://imgur.com/foobar2.jpg\nCapital: Reykjavik\n']
For writing the CSV, Python has standard library functions.
>>> import csv
>>> csvfile = open('foo.csv', 'wb')
>>> fieldnames = ['Country', 'Link', 'Capital']
>>> writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
>>> for entry in entries:
... cols = entry.strip().splitlines()
... writer.writerow({'Country': cols[0], 'Link':cols[1].split(': ')[1], 'Capital':cols[2].split(':')[1]})
...
If your data is more semi structured or badly formatted, consider using a library like PyParsing.
Edit:
Second column contains URLs, so we need to handle the splits well.
>>> cols[1]
'Link: http://imgur.com/foobar2.jpg'
>>> cols[1].split(':')[1]
' http'
>>> cols[1].split(': ')[1]
'http://imgur.com/foobar2.jpg'
The way that I would do that would be to use the open() function using the syntax of:
f = open('NameOfFile.extensionType', 'a+')
Where "a+" is append mode. The file will not be overwritten and new data can be appended. You could also use "r+" to open the file in read mode, but would lose the ability to edit. The "+" after a letter signifies that if the document does not exist, it will be created. The "a+" I've never found to work without the "+".
After that I would use a for loop like this:
data = []
tmp = []
for line in f:
line.strip() #Removes formatting marks made by python
if line == '~':
data.append(tmp)
tmp = []
continue
else:
tmp.append(line)
Now you have all of the data stored in a list, but you could also reformat it as a class object using a slightly different algorithm.
I have never edited CSV files using python, but I believe you can use a loop like this to add the data:
f2 = open('CSVfileName.csv', 'w') #Can change "w" for other needs i.e "a+"
for entry in data:
for subentry in entry:
f2.write(str(subentry) + '\n') #Use '\n' to create a new line
From my knowledge of CSV that loop would create a single column of all of the data. At the end remember to close the files in order to save the changes:
f.close()
f2.close()
You could combine the two loops into one in order to save space, but for the sake of explanation I have not.

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.

Python CSV library returning 1 item instead of a list of items

Im trying to use the CSV library to do some excel processing, but when I use the code posted below, row returns the entirety of data as 1 item, so row[0] returns the entire file and row[1] returns index out of range. Is there a way to make each row a list with each cell being an item? Making the final product a list of lists. I was thinking of using split everytime ther was a close bracket ']' . If needed I can post the excel file
Heres a sample of what some of the output looks like. This is all one item in the list:
['3600035671,"$13,668",8/11/2008,8/11/2013,,,2,4A,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,']
['3910435005,"$34,872",4/1/2010,10/8/2016,,,2,4A,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,']
['5720636344,"$1,726",8/30/2010,9/5/2011,,,3,6C,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,']
['15260473510,"-$1,026,580",7/22/2005,3/5/2008,,,6,1C2A,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,']
import csv
csvfile = open('Invictus.csv', 'rU')
data = csv.reader(csvfile, dialect=csv.excel_tab)
for char in data:
char = filter(None, char)
print char
Assuming you are giving examples of your data above the line import csv, it looks like your data is comma delimited but you are setting up your CSV reader to expect tab delimited data (dialect=csv.excel_tab).
What happens if you change that line to:
data = csv.reader(csvfile, dialect=csv.excel)

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.

Categories

Resources