I have a CSV file which contains an Address field. The CSV file has Addresses outlined as the following in everything caps but I need your assistance in using the title() snippet on the append(row[1]). I have tried doing:
append.title(row[1]) but it does not work.
In the CSV File --------------Needs to be:
1234 PRESTON ROAD -------- 1234 Preston Road
1245 JACKSON STREET ------- 1245 Jackson Street
8547 RAINING COURT ------- 8547 Raining Court
with open('C:\\Users\\Jake\\Desktop\\My Files\\Python Files\\PermitData.csv', 'rb') as f:
reader = csv.reader(f)
next (reader)
data = list(reader)
PermitData = []
for row in data:
PermitData.append(row[0]),PermitData.append(row[1]),PermitData.append(row[2]),
PermitData.append(row[3]),PermitData.append(row[4]),PermitData.append(row[5]),
PermitData.append(row[6])
results = PermitData
for result in results:
print result
f.close()
The reason I am iterating over every row in the CSV file is the need to save the edited CSV file as a temp file before replacing the original with the edited one. I am not that articulate with Python as I am learning by doing actual projects so please forgive any stupidity in the question and coding. Please provide your kind advice and assistance.
The following code will create a new file named output.csv with the output that you asked for:
import csv
with open('C:\\Users\\Jake\\Desktop\\My Files\\Python Files\\output.csv', 'w') as out:
with open('C:\\Users\\Jake\\Desktop\\My Files\\Python Files\\PermitData.csv', 'rb') as f:
reader = csv.reader(f)
out.write(next(reader)[0].replace('\t', ' ') + '\n')
data = list(reader)
for item in data:
item = item[0].split(' ')
out.write(' '.join(
[item[0],
item[1].title(),
item[2].title()]) + '\n')
If what you want is just to print the result, try as follows:
import csv
results = []
with open('permitData.csv', 'rb') as f:
reader = csv.reader(f)
next(reader)
data = list(reader)
for item in data:
item = item[0].split(' ')
results.append(' '.join(
[item[0],
item[1].title(),
item[2].title()]))
Output:
>>> for result in results:
... print result
...
1234 Preston Road
1245 Jackson Street
8547 Raining Court
You'll need to call title() on the string you want to convert to Title Case. This should work:
append(row[1].title())
Try this:
PermitData = []
with open('C:\\Users\\Jake\\Desktop\\My Files\\Python Files\\PermitData.csv', 'rb') as f:
reader = csv.reader(f)
headers = next (reader)
for row in reader:
row = row[:1]+ [row[1].title()] + row[2:] # Assuming row[1] is your address field you need in a title case.
PermitData.append(row)
for result in PermitData:
print result
You should also note that you don't need to call f.close() when you are using the with syntax for opening a file.
The file gets closed automatically once you exit with
Related
I have a CSV file whose structure looks something like this:
Name Nation Location URL
Electra European Luton, Dunstable, Stevenage //ur1l/, //url2/, //url3/
Bob British Bedford, Bedfordshire //ur1l/, //url21/, //url13/
Lyndi Thai Liverpool Street, Street, Goodge Street //ur11l/, //url12/, //url3/
I am trying to access each link separately, but my attempts return each letter separately:
My code:
with open('csv/users.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
next(csv_reader)
for line in csv_reader:
print(line) #ok
for link in line[3]:
print(link) # it's return each letter separately
How do you get each link separately?
The fields in the file are separated by tabs, and multiple values by ', ' so I doubt that even line or line[3] contain the values you expect.
You should handle the file accordingly:
with open('csv/users.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter='\t')
next(csv_reader)
for line in csv_reader:
for link in line[3].split(', '):
print(link)
BTW, instead of skipping the header row with next(csv_reader) and then using unreadable line[3], you can use DictReader to be able to access columns by name:
import csv
with open('csv/users.csv') as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter='\t')
for line in csv_reader:
for link in line['URL'].split(', '):
print(link)
I did a python script to access a site, and on that site do a certain search for me to do a scan of the search result.
I write the return of the result as txt
clear = self.browser.find_elements_by_class_name('Whitebackground')
for scrape in clear:
with open('result.txt', 'a') as writer:
writer.write(scrape.text)
writer.write('\n')
writer.close()
I want to return the result in CSV to open in Excel
clear = self.browser.find_elements_by_class_name('Whitebackground')
for scrape in clear:
with open('result.csv', 'a') as writer:
writer.write(scrape.text)
writer.write('\n')
writer.close()
My problem is that I have to fill 4 columns
I get my current result that way
656473930362736
The car needs to change the oil
Model: sedan
type of fuel: Gasoline
I want to receive my result in CSV in this way
'Number'; 'description'; 'Model'; 'Type of fuel'
6564...; The car needs..; sedan ; Gasoline
'Number', 'description', 'Model', 'Type of fuel' would be the titles by columns
'6564...', 'The car needs...', 'sedan', 'Gasoline' Would be the rows of the columns
does anyone have any idea how I can do this??
if you can convert your data into dictionaries, its super easy:
data = []
datapoint = {}
datapoint['Number'] = 656473930362736
datapoint['Description'] = "The car needs to change the oil."
datapoint['Model'] = "Sedan"
datapoint['Type of Fuel'] = "Gasoline"
data.append(datapoint)
fieldnames = ['Number','Description','Model','Type of Fuel']
def filewriter(filename, data, fieldnames):
with open (filename, "w", newline='', encoding='utf-8-sig') as csvfile:
csvfile = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=',', dialect='excel')
csvfile.writeheader()
for row in data:
csvfile.writerow(row)
filewriter("out.csv", data, fieldnames)
converting your data into dictionaries is a separate problem, but should be no big deal if your data is structured well.
Simply parse your text by splitting into elements:
txt = "656473930362736\n" \
"The car needs to change the oil\n" \
"Model: sedan\n" \
"type of fuel: Gasoline"
list_of_elements = txt.split('\n')
required_text = list_of_elements[0] + ';' + list_of_elements[1] + ';' list_of_elements[2].split(':')[1] + ';' + list_of_elements[3].split(':') + ';'
file.write(required_text + '\n')
So I have a .csv file with names, lat, lon, and phone number; separated by comma. I need to open the file, edit the phone number into a more legible format and then write that back to the file.
There is a nice solution to editing the phone numbers Here
Not really sure of the best way to approach this problem. Any help is greatly appreciated. Thanks.
import csv
def phone_format(n):
return format(int(n[:-1]), ",").replace(",", "-") +n[-1]
with open('sample.csv', 'rU') as csvfile:
spotreader = csv.reader(csvfile)
spotwriter = csv.writer(csvfile)
for row in spotreader:
spotwriter.writerow([0] + phone_format(spotreader[1]))
This does not work. Not really sure how to get what I am looking for.
Sample of my csv file below
Jason Schow,,5016098648
Dena VanGorder,,6074621816
Lindsey McNabb,3066533971,3066505001
Jeff Wozniak,3066531566,3069420647
Victoria Norton,3067692840,3067697062
Benjie Butikofer,3067692107,3067697108
Jessica Duede,,3062813158
Pete Vogeh,3063776261,3069890349
Melissa Kwasney,,3069412583
Type of output to .csv file that I am looking for below:
Jason Schow,,501-609-8648
Dena VanGorder,,607-462-1816
Lindsey McNabb,306-653-3971,306-650-5001
Jeff Wozniak,306-653-1566,306-942-0647
Victoria Norton,306-769-2840,306-769-7062
Benjie Butikofer,306-769-2107,306-769-7108
Jessica Duede,,306-281-3158
Pete Vogeh,306-377-6261,306-989-0349
Melissa Kwasney,,306-941-2583
Shouldn't you be formatting on row, instead of spotreader? This works fine for me reading it from a .csv file. I added a check for ignoring empty strings. Also the spotwriter won't work since you aren't opening the file in write mode. 'rU' is read mode only. What you want to do is make a brand new .csv file and write the output there like this:
import csv
def phone_format(n):
return format(int(n[:-1]), ",").replace(",", "-") +n[-1]
with open('sample.csv', 'rU') as csvfile:
with open('sampleOutput.csv', 'w') as csvfile2:
spotreader = csv.reader(csvfile)
spotwriter = csv.writer(csvfile2)
for row in spotreader:
if row[1] != '':
spotwriter.writerow([row[0], phone_format(row[1]), row[2]])
else:
spotwriter.writerow([row[0], row[1], row[2]])
The input is your .csv file.
Output:
Jason Schow,,5016098648
Dena VanGorder,,6074621816
Lindsey McNabb,306-653-3971,3066505001
Jeff Wozniak,306-653-1566,3069420647
Victoria Norton,306-769-2840,3067697062
Benjie Butikofer,306-769-2107,3067697108
Jessica Duede,,3062813158
Pete Vogeh,306-377-6261,3069890349
Melissa Kwasney,,3069412583
This is assuming you only want to edit the first phone number, if you want to edit the second phone number too, you will have to do some additional programming.
Got it figured out. I stumbled across this in another stackoverflow article. Article Here
The corrected code is as follows. Thanks again for all the help and suggestions.
import csv
def phone_format(n):
return format(int(n[:-1]), ",").replace(",", "-") +n[-1]
with open('sample.csv', 'rU') as csvfile:
with open('sampleOutput.csv', 'w', newline='') as csvfile2:
spotreader = csv.reader(csvfile)
spotwriter = csv.writer(csvfile2)
for row in spotreader:
if row[1] != '' and row[2] != '':
spotwriter.writerow([row[0], phone_format(row[1]), phone_format(row[2])])
elif row[1] != '' and row[2] == '':
spotwriter.writerow([row[0], phone_format(row[1]), (row[2])])
elif row[1] == '' and row[2] != '' :
spotwriter.writerow([row[0], (row[1]), phone_format(row[2])])
Some example data:
title1|title2|title3|title4|merge
test|data|here|and
test|data|343|AND
",3|data|343|and
My attempt at coding this:
import csv
import StringIO
storedoutput = StringIO.StringIO()
fields = ('title1', 'title2', 'title3', 'title4', 'merge')
with open('file.csv', 'rb') as input_csv:
reader = csv.DictReader(input_csv, fields, delimiter='|')
for counter, row in enumerate(reader):
counter += 1
#print row
if counter != 1:
for field in fields:
if field == "merge":
row['merge'] = ("%s%s%s" % (row["title1"], row["title3"], row["title4"]))
print row
storedoutput.writelines(','.join(map(str, row)) + '\n')
contents = storedoutput.getvalue()
storedoutput.close()
print "".join(contents)
with open('file.csv', 'rb') as input_csv:
input_csv = input_csv.read().strip()
output_csv = []
output_csv.append(contents.strip())
if "".join(output_csv) != input_csv:
with open('file.csv', 'wb') as new_csv:
new_csv.write("".join(output_csv))
Output should be
title1|title2|title3|title4|merge
test|data|here|and|testhereand
test|data|343|AND|test343AND
",3|data|343|and|",3343and
For your reference upon running this code the first print it prints the rows as I would hope then to appear in the output csv. However the second print prints the title row x times where x is the number of rows.
Any input or corrections or working code would be appreciated.
I think we can make this a lot simpler. Dealing with the rogue " was a bit of a nuisance, I admit, because you have to work hard to tell Python you don't want to worry about it.
import csv
with open('file.csv', 'rb') as input_csv, open("new_file.csv", "wb") as output_csv:
reader = csv.DictReader(input_csv, delimiter='|', quoting=csv.QUOTE_NONE)
writer = csv.DictWriter(output_csv, reader.fieldnames, delimiter="|",quoting=csv.QUOTE_NONE, quotechar=None)
merge_cols = "title1", "title3", "title4"
writer.writeheader()
for row in reader:
row["merge"] = ''.join(row[col] for col in merge_cols)
writer.writerow(row)
produces
$ cat new_file.csv
title1|title2|title3|title4|merge
test|data|here|and|testhereand
test|data|343|AND|test343AND
",3|data|343|and|",3343and
Note that even though you wanted the original file updated, I refused. Why? It's a bad idea, because then you can destroy your data while working on it.
How can I be so sure? Because that's exactly what I did when I first ran your code, and I know better. ;^)
That double quote in the last line is definitely messing up the csv.DictReader().
This works:
new_lines = []
with open('file.csv', 'rb') as f:
# skip the first line
new_lines.append(f.next().strip())
for line in f:
# the newline and split the fields
line = line.strip().split('|')
# exctract the field data you want
title1, title3, title4 = line[0], line[2], line[3]
# turn the field data into a string and append in to the rest
line.append(''.join([title1, title3, title4]))
# save the new line for later
new_lines.append('|'.join(line))
with open('file.csv', 'w') as f:
# make one long string and write it to the new file
f.write('\n'.join(new_lines))
import csv
import StringIO
stored_output = StringIO.StringIO()
with open('file.csv', 'rb') as input_csv:
reader = csv.DictReader(input_csv, delimiter='|', quoting=csv.QUOTE_NONE)
writer = csv.DictWriter(stored_output, reader.fieldnames, delimiter="|",quoting=csv.QUOTE_NONE, quotechar=None)
merge_cols = "title1", "title3", "title4"
writer.writeheader()
for row in reader:
row["merge"] = ''.join(row[col] for col in merge_cols)
writer.writerow(row)
contents = stored_output.getvalue()
stored_output.close()
print contents
with open('file.csv', 'rb') as input_csv:
input_csv = input_csv.read().strip()
if input_csv != contents.strip():
with open('file.csv', 'wb') as new_csv:
new_csv.write("".join(contents))
I'm trying to iterate over a CSV file that has a 'master list' of names, and compare it to another CSV file that contains only the names of people who were present and made phone calls.
I'm trying to iterate over the master list and compare it to the names in the other CSV file, take the number of calls made by the person and write a new CSV file containing number of Calls if the name isn't found or if it's 0, I need that column to have 0 there.
I'm not sure if its something incredibly simple I'm overlooking, or if I am truly going about this incorrectly.
Edited for formatting.
import csv
import sys
masterlst = open('masterlist.csv')
comparelst = open(sys.argv[1])
masterrdr = csv.DictReader(masterlst, dialect='excel')
comparerdr = csv.DictReader(comparelst, dialect='excel')
headers = comparerdr.fieldnames
with open('callcounts.csv', 'w') as outfile:
wrtr = csv.DictWriter(outfile, fieldnames=headers, dialect='excel', quoting=csv.QUOTE_MINIMAL, delimiter=',', escapechar='\n')
wrtr.writerow(dict((fn,fn) for fn in headers))
for lines in masterrdr:
for row in comparerdr:
if lines['Names'] == row['Names']:
print(lines['Names'] + ' has ' + row['Calls'] + ' calls')
wrtr.writerow(row)
elif lines['Names'] != row['Names']:
row['Calls'] = ('%s' % 0)
wrtr.writerow(row)
print(row['Names'] + ' had 0 calls')
masterlst.close()
comparelst.close()
Here's how I'd do it, assuming the file sizes do not prove to be problematic:
import csv
import sys
with open(sys.argv[1]) as comparelst:
comparerdr = csv.DictReader(comparelst, dialect='excel')
headers = comparerdr.fieldnames
names_and_counts = {}
for line in comparerdr:
names_and_counts[line['Names']] = line['Calls']
# or, if you're sure you only want the ones with 0 calls, just use a set and only add the line['Names'] values that that line['Calls'] == '0'
with open('masterlist.csv') as masterlst:
masterrdr = csv.DictReader(masterlst, dialect='excel')
with open('callcounts.csv', 'w') as outfile:
wrtr = csv.DictWriter(outfile, fieldnames=headers, dialect='excel', quoting=csv.QUOTE_MINIMAL, delimiter=',', escapechar='\n')
wrtr.writerow(dict((fn,fn) for fn in headers))
# or if you're on 2.7, wrtr.writeheader()
for line in masterrdr:
if names_and_counts.get(line['Names']) == '0':
row = {'Names': line['Names'], 'Calls': '0'}
wrtr.writerow(row)
That writes just the rows with 0 calls, which is what your text description said - you could tweak it if you wanted to write something else for non-0 calls.
Thanks everyone for the help. I was able to nest another with statement inside of my outer loop, and add a variable to test whether or not the name from the master list was found in the compare list. This is my final working code.
import csv
import sys
masterlst = open('masterlist.csv')
comparelst = open(sys.argv[1])
masterrdr = csv.DictReader(masterlst, dialect='excel')
comparerdr = csv.DictReader(comparelst, dialect='excel')
headers = comparerdr.fieldnames
with open('callcounts.csv', 'w') as outfile:
wrtr = csv.DictWriter(outfile, fieldnames=headers, dialect='excel', quoting=csv.QUOTE_MINIMAL, delimiter=',', escapechar='\n')
wrtr.writerow(dict((fn,fn) for fn in headers))
for line in masterrdr:
found = False
with open(sys.argv[1]) as loopfile:
looprdr = csv.DictReader(loopfile, dialect='excel')
for row in looprdr:
if row['Names'] == line['Names']:
line['Calls'] = row['Calls']
wrtr.writerow(line)
found = True
break
if found == False:
line['Calls'] = '0'
wrtr.writerow(line)
masterlst.close()
comparelst.close()