I have a csv file with say 3 rows like this:
Dallas
Houston
Ft. Worth
What I want to do is be able to read those in and make links out of them but have all the lines output on one line. Example output would need to be like this:
Dallas Houston Ft. Worth
Here is the code I have thus far and it reads the csv file and outputs but it creates different rows, and I only want one row plus I need to append the html code for hyper links in.
f_in = open("data_files/states/major_cities.csv",'r')
for line in f_in.readlines():
f_out.write(line.split(",")[0]+"")
f_in.close()
f_out.close()
That's because each line in f_in.readlines() comes with a newline tacked on to the end. (Try adding a print(repr(line)) in that loop). What you need to do is remove that newline before write ing to f_out:
for line in f_in.readlines():
actual_line = line.rstrip('\n')
Your entire code would look like this:
import re
with open('data_files/states/major_cities.csv') as f_in:
with open('output_file.csv', 'w') as f_out:
for line in f_in:
city = line.rstrip('\n')
f_out.write('{}'.format(
re.sub(r'\W+', '-', city.lower()),
city
))
The with statements take care of closeing files, so you don't need those last two lines.
UPDATE
As J.F. Sebastian pointed out, it's also necessary to slugify the city name to achieve the output you want.
Try the python CSV module for handling CSV files
import csv
file_out = open('file.txt','w')
with open('example.csv','rb') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
col=row[0]
str="<a href=/" + col.strip().lower()
str+= "/>" + col + "</a> "
file_out.write(str)
Related
I'd like to create a CSV from a TXT file. I have a text file with lines (300 lines+) separated by backslashes. I'd like each line to be a separate row, and each backslash to be a separate new column.
The text file looks like:
example 1\example 2\example 3\example 4
test 1\test 2\test 3\test 4
I'd like the CSV to look like:
Example 1
Example 2
Example 3
Example 4
Test 1
Test 2
Test 3
Test 4
So far I have:
import csv
with open('Report.txt') as report:
report_txt = report.read()
with open('Report.csv','w',newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(report_txt)
I know I need to use \ as a delimiter, but I'm not sure how. Thanks for any help!
Define your delimiter like this (escape the \):
reader = csv.reader(open("Report.csv"), delimiter="\\")
Code:
import csv
with open('Report.txt') as report:
reader = csv.reader(report, delimiter="\\")
with open('Report_output.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for line in reader:
writer.writerow(line)
First you got to split the string based on the delimeter. You can achieve this by using the split operator or regex.
import csv
with open('file.txt', 'r') as in_file:
stripped = (line.strip() for line in in_file)
lines = (line.split("\\") for line in stripped if line)
Then pretty much write it to the csv.
with open('report.csv', 'w') as out_file:
writer = csv.writer(out_file)
writer.writerows(lines)
Tweak your code accordingly. The concept is pretty much the same. Note the double backslash is to account for the escape character.
If you are just trying to convert that text into CSV, you can just replace every "\" character with ";" and you'll have a valid CSV file.
Else, if you want to do something with the parsed data before reexporting to CSV, you can read the file line by line and use the split() Method with "\", then rejoin and write line by line, like here:
with open('in.txt') as input_file:
with open('out.csv','a') as output_file:
txt_line = input_file.readline()
while txt_line:
cells = txt_line.split("\\")
# Do something with each cell...
csv_line = ";".join(cells)
output_file.write(csv_line)
txt_line = input_file.readline()
I have not managed to achieve it. but what confuses me the most is how to add an element in a line "n" of my csv, for example I want to add a line in the line 2 of my csv.
mycsv.csv
name,last name
yeison, smith
lola, boa
elmo, spitia
anderson, exneider
juan, ortega
this is my code:
with open('mycsv.csv', 'w') as f:
#I need add "barney, cubides" on position [2] of my csv
f.write("barney, cubides") #not works properly..
how can do it?
You have to write to file after you read it. So read the whole csv and save each line as a list, insert your new line where you want to, and then re-write the whole file.
index_to_insert = 2
new_csv = []
new_line = "barney, cubides\n"
with open("mycsv.csv", "r") as f:
new_csv = f.readlines()
new_csv.insert(index_to_insert, new_line)
with open("mycsv.csv", "w") as f:
for line in new_csv:
f.write(line)
ps. You might want to get rid of the whitespaces before and after the commas in your csv file.
I have a CSV file where I am looping and matching with my database for getting results according to these matches.
I encountered a problem in the case where there is a space at the end of the text. So I did my research and found that I need to add the rstrip function to remove spaces at the end of the text.
Here is my code:
with open(path, encoding='utf-8') as f:
data = csv.reader(f, delimiter='|')
for row in data:
line = row[0]
cleanline = line.rstrip()
lines.append(cleanline)
query = line
The code is not working. I tried also strings like /s or strip, and replace functions as well but nothing is working. What can be the reason? What am I doing wrong?
CSV File with empty space at the end:
Sistem en az 23.8 inç boyutlarında olmalıdır.
1 adet HDMI port olmalıdır.
You could try the following approach:
import csv
path = 'input.csv'
lines = []
with open(path, newline='', encoding='utf-8') as f:
data = csv.reader(f, delimiter='|', skipinitialspace=True)
for row in data:
lines.append([c.strip() for c in row])
print(lines)
This removes all leading and trailing spaces from each cell in a row using the strip() command. Depending on your data, it might be just enough to add the additional skipinitialspace=True parameter. This though would not remove trailing spaces before the next delimiter. newline='' should also be used in Python 3.x when used with a csv.reader().
The file you have given just contains lines of text, as such you could read it as follows:
lines = []
with open('input.csv', encoding='utf-8') as f_input:
for line in f_input:
lines.append(line.strip())
print(lines)
This would give you lines containing:
['Sistem en az 23.8 inç boyutlarında olmalıdır.', '1 adet HDMI port olmalıdır.']
I'm trying to write to a CSV file with output that looks like this:
14897,40.50891,-81.03926,168.19999
but the CSV writer keeps writing the output with quotes at beginning and end
'14897,40.50891,-81.03926,168.19999'
When I print the line normally, the output is correct but I need to do line.split() or else the csv writer puts output as 1,4,8,9,7 etc...
But when I do line.split() the output is then
['14897,40.50891,-81.03926,168.19999']
Which is written as '14897,40.50891,-81.03926,168.19999'
How do I make the quotes go away? I already tried csv.QUOTE_NONE but doesn't work.
with open(results_csv, 'wb') as out_file:
writer = csv.writer(out_file, delimiter=',')
writer.writerow(["time", "lat", "lon", "alt"])
for f in file_directory):
for line in open(f):
print line
line = line.split()
writer.writerow(line)
with line.split(), you're not splitting according to commas but to blanks (spaces, linefeeds, tabs). Since there are none, you end up with only 1 item per row.
Since this item contains commas, csv module has to quote to make the difference with the actual separator (which is also comma). You would need line.strip().split(",") for it to work, but...
using csv to read your data would be a better idea to fix this:
replace that:
for line in open(some_file):
print line
line = line.split()
writer.writerow(line)
by:
with open(some_file) as f:
cr = csv.reader(f) # default separator is comma already
writer.writerows(cr)
You don't need to read the file manually. You can simply use csv reader.
Replace the inner for loop with:
# with ensures that the file handle is closed, after the execution of the code inside the block
with open(some_file) as file:
row = csv.reader(file) # read rows
writer.writerows(row) # write multiple rows at once
I have a movie dataset that looks like this:
1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
2,Jumanji (1995),Adventure|Children|Fantasy
3,Grumpier Old Men (1995),Comedy|Romance
4,Waiting to Exhale (1995),Comedy|Drama|Romance
5,Father of the Bride Part II (1995),Comedy
6,Heat (1995),Action|Crime|Thriller
7,Sabrina (1995),Comedy|Romance
8,Tom and Huck (1995),Adventure|Children
I want to extract only the last part (genres part, e.g, Adventure|Animation|Children|Comedy|Fantasy) and store them in a list list[Adventure, Animation, Children, Comedy, Fantasy]. However, I am still stuck at slicing step. I don't know how to do that since line[:-1] doesn't slice. I use Python 2.7
with open(path + 'movie.csv') as f:
for line in f:
print line[:-1]
with open(path + 'movie.csv') as f:
for line in f:
print line.split(',')[:-1].rstrip('\n').split('|')
Your slice will return the last character of each line, since the lines are not splitted when you read the file in regular manner. You should read the file using csv module that separates the lines automatically with ',' delimiter. Then split the result with |.
import csv
with open(path + 'movie.csv') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
print(row[-1].split('|'))