Change CSV from writing in one column to multiple columns - python

I am attempting to write items that I feed my code with a for loop into a csv. I am able to write it to a csv however it throws everything into a single column like so...The output of my code. I've achieved this using this code:
workspace = arcpy.mapping.MapDocument("CURRENT")
with open (arcpy.GetParameterAsText(0),"wb") as csv_file:
writer = csv.writer(csv_file, delimiter = ',')
for textElement in arcpy.mapping.ListLayoutElements(workspace, "TEXT_ELEMENT","elem*"):
writer.writerow([textElement.name],)
writer.writerow([textElement.text],)
The issue I have is that I want to push each new instance of "elem" into a new column. If anyone can help me write some code to create a csv that looks like this... desired csv I would greatly appreciate it.

It looks like you need to do:
writer.writerow([textElement.name,textElement.text])
It would be easier to help if you provided something we could run. See: https://stackoverflow.com/help/mcve

I found this example a while back:
import csv
with open(newfilePath, "w") as f:
writer = csv.writer(f)
for row in rows:
writer.writerow(row)
I work in pandas a lot and this has worked great for me!

I created a dictionary of all of the elements (elem1, elem2, elem3) and the content. Than I just write the dictionary into the csv rather than write each individual element and content. I used this code:
dic = {}
workspace = arcpy.mapping.MapDocument(r"path.mxd")
for textElement in arcpy.mapping.ListLayoutElements(workspace, "TEXT_ELEMENT", "elem*"):
name = ''.join(textElement.name)
content = ''.join(textElement.text)
dic[str(name)] = str(content)
print dic
with open(r'test.csv', 'wb') as csv_file:
writer = csv.writer(csv_file, delimiter = ',')
for item in dic.items():
writer.writerow(item)

Related

Reading a CSV and writing the exact file back with one column changed

I'm looking to read a CSV file and make some calculations to the data in there (that part I have done).
After that I need to write all the data into a new file Exactly the way it is in the original with the exception of one column which will be changed to the result of the calculations.
I can't show the actual code (confidentiality issues) but here is an example of the code.
headings = ["losts", "of", "headings"]
with open("read_file.csv", mode="r") as read,\
open("write.csv", mode='w') as write:
reader = csv.DictReader(read, delimiter = ',')
writer = csv.DictWriter(write, fieldnames=headings)
writer.writeheader()
for row in reader:
writer.writerows()
At this stage I am just trying to return the same CSV in "write" as I have in "read"
I haven't used CSV much so not sure if I'm going about it the wrong way, also understand that this example is super simple but I can't seem to get my head around the logic of it.
You're really close!
headings = ["losts", "of", "headings"]
with open("read_file.csv", mode="r") as read, open("write.csv", mode='w') as write:
reader = csv.DictReader(read, delimiter = ',')
writer = csv.DictWriter(write, fieldnames=headings)
writer.writeheader()
for row in reader:
# do processing here to change the values in that one column
processed_row = some_function(row)
writer.writerow(processed_row)
Why you don't use pandas?
import pandas as pd
csv_file = pd.read_csv('read_file.csv')
# do_something_and_add_a_column_here()
csv_file.to_csv('write_file.csv')

how to select a specific column of a csv file in python

I am a beginner of Python and would like to have your opinion..
I wrote this code that reads the only column in a file on my pc and puts it in a list.
I have difficulties understanding how I could modify the same code with a file that has multiple columns and select only the column of my interest.
Can you help me?
list = []
with open(r'C:\Users\Desktop\mydoc.csv') as file:
for line in file:
item = int(line)
list.append(item)
results = []
for i in range(0,1086):
a = list[i-1]
b = list[i]
c = list[i+1]
results.append(b)
print(results)
You can use pandas.read_csv() method very simply like this:
import pandas as pd
my_data_frame = pd.read_csv('path/to/your/data')
results = my_data_frame['name_of_your_wanted_column'].values.tolist()
A useful module for the kind of work you are doing is the imaginatively named csv module.
Many csv files have a "header" at the top, this by convention is a useful way of labeling the columns of your file. Assuming you can insert a line at the top of your csv file with comma delimited fieldnames, then you could replace your program with something like:
import csv
with open(r'C:\Users\Desktop\mydoc.csv') as myfile:
csv_reader = csv.DictReader(myfile)
for row in csv_reader:
print ( row['column_name_of_interest'])
The above will print to the terminal all the values that match your specific 'column_name_of_interest' after you edit it to match your particular file.
It's normal to work with lots of columns at once, so that dictionary method of packing a whole row into a single object, addressable by column-name can be very convenient later on.
To a pure python implementation, you should use the package csv.
data.csv
Project1,folder1/file1,data
Project1,folder1/file2,data
Project1,folder1/file3,data
Project1,folder1/file4,data
Project1,folder2/file11,data
Project1,folder2/file42a,data
Project1,folder2/file42b,data
Project1,folder2/file42c,data
Project1,folder2/file42d,data
Project1,folder3/filec,data
Project1,folder3/fileb,data
Project1,folder3/filea,data
Your python program should read it by line
import csv
a = []
with open('data.csv') as csv_file:
reader = csv.reader(csv_file, delimiter=',')
for row in reader:
print(row)
# ['Project1', 'folder1/file1', 'data']
If you print the row element you will see it is a list like that
['Project1', 'folder1/file1', 'data']
If I would like to put in my list all elements in column 1, I need to put that element in my list, doing:
a.append(row[1])
Now in list a I will have a list like:
['folder1/file1', 'folder1/file2', 'folder1/file3', 'folder1/file4', 'folder2/file11', 'folder2/file42a', 'folder2/file42b', 'folder2/file42c', 'folder2/file42d', 'folder3/filec', 'folder3/fileb', 'folder3/filea']
Here is the complete code:
import csv
a = []
with open('data.csv') as csv_file:
reader = csv.reader(csv_file, delimiter=',')
for row in reader:
a.append(row[1])

Read a csv file, then sort it, then rewrite it

I want to read in a csv file, sort it, then rewrite a new file. Any help?
You should probably take a look at the python documentation of the csv module:
https://docs.python.org/3.6/library/csv.html
You could also use pandas, but that might be overkill if you're new to python.
To give you some initial code to play with:
# file test.csv
2,a,x
0,b,y
1,c,z
Code:
import csv
csv_lines = []
# read csv
with open('test.csv') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
csv_lines.append(row)
# sort by first column
csv_lines_sorted = sorted(csv_lines, key=lambda x: x[0])
# write csv
with open('test_sorted.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
for row in csv_lines_sorted:
writer.writerow(row)

Retrieve columns from a csv files and extract value to another csv file

I have a csv file as follow:
lat,lon,date,data1,data2
1,2,3,4,5
6,7,8,9,10
From this csv file I want to retrieve and extract the column date and data1 to another csv file. I have the following code:
import csv
os.chdir(mydir)
column_names = ["date", "data1"]
index=[]
with open("my.csv", "r") as f:
mycsv = csv.DictReader(f)
for row in mycsv:
for col in column_names:
try:
data=print(row[col])
with open("test2.txt", "w") as f:
print(data, file=f)
except KeyError:
pass
Unfortunately, the output is a file with a "none" on it... Does anyone knows how to retrieve and write to another file the data I wish to use?
There are a few issues with your code:
Everytime you open("test2.txt", "w"), w option will open your file and delete all its contents.
You are storing return value or print, which is None and then trying to print this into yout file
Read your CSV into a list of dict's, as below:
import csv
with open('your_csv.csv') as csvfile:
reader = csv.DictReader(csvfile)
read_l = [{key:value for key, value in row.items() if key in ('date', 'data1')}
for row in reader]
and then use DictWriter to write to a new CSV.
with open('new.csv', 'w') as csvfile:
fieldnames = read_l[0].keys()
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in read_l[1:]:
writer.writerow(row)
Try with below steps may help you. But they require pandas library.Install pandas library before you go for below steps. input.csv contains data that you have mentioned.
import pandas as pd
df=pd.read_csv('input.csv')
df_new=df.iloc[0:,2:4]
df_new.to_csv("output.csv",index=False)
The reason why you see None in your file is because you're assigning the result of print(row[col]) to your data variable:
data=print(row[col])
print() doesn't return anything, therefore the content of data is None. If you remove the print() and just have data = row[col], you will get something valuable.
There is one more issue that I see in your code, which you probably want to get fixed:
You're opening the file over and over again with each iteration in the first loop. Therefore, with each row you're overwriting the entire file with that rows value. If you want the entire column, then you'd have open the file once, before the loop.
I will recommend you should use panda. I haven't run this script but something like this should work.
import panda as pd
import csv
frame = pd.read_csv('my.csv')
df=frame[['date','data2']]
with open('test2.csv', 'a', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(df)
import pandas as pd
df = pd.read_csv("my.csv") #optional "header"=True
new_df = df[["date","data1"]]
new_df.to_csv("new_csv_name.csv")
#if you don't need index
new_df.to_csv('new_csv_name.csv', index=False)

How to store data from a list in a .csv file?

A list b is like:
X = [((a,b),12),((c,d),34),...]
for i in range(0,5):
print X[i][0][0],X[i][1],X[i][0][1]
I want to save my data in a csv file. Eg. This can be saved as :
a,12,b- in first row (which is X[0][0][0], X[0][1])
c,34,d - in my second row and so on.
This is part of my project and I have no idea about .csv file.
What I did
writer= csv.writer(open('dict.csv','wb'))
for i in range(0,5):
writer.writerow([x[i][0][0],x[i][1],x[i][0][1]])
But couldn't find anything in the csv file. How to fix it?
You do not close the file:
f = open('dict.csv','wb')
writer= csv.writer(f)
for i in range(0,5):
writer.writerow([x[i][0][0],x[i][1])
f.close() # close the file
Or better use the answer with the with statement:
with open('dict.csv','wb') as outfile:
writer = csv.writer(outfile)
for i in range(0,5):
writer.writerow([x[i][0][0],x[i][1])
You were almost there.
while you write to csv , will default delimiter.
So just provide the data that should be written to csv.
import csv
x= [(('a','b'),12),(('c','d'),34)]
writer= csv.writer(open('dict.csv','wb'))
for i in range(0,2):
writer.writerow([x[i][0][0]]+[x[i][1]])
Check this for more info. https://docs.python.org/2/library/csv.html

Categories

Resources