Please help, I can't find it.
Why can I append row but not row[2]? It crashes. I'm using Python 3.4.3.
import csv
with open("file.csv", encoding="UTF8") as csvfile:
read = csv.reader(csvfile, delimiter=";")
original = []
for row in read:
original.append(row[2])
csvfile.close()
print(original)
Thanks
This looks to be a frustrating debugging experience.
One possibility is that there's a last line in the file that has only one item which is causing the issue.
A quick way to look at the situation (depending how long your file is) might be to throw in a print and see what's going on, line by line:
for row in read:
try:
original.append(row[2])
except:
print(row)
If you run with this, you may be able to see what happens just before the crash.
You may want to be a little more descriptive on what the crash is. It's famously difficult to help with such a vague description. A little more effort will help people to help you more effectively.
I would suggest you do not try and print the whole of your CSV list at the end, this can cause some IDEs to lock up for a long time.
Instead you could just print the last few entries to prove it has worked:
print("Rows read:", len(original)
print(original[-10:])
Related
The result is printed correctly, but the csv file stops at the first iteration and repeats itself.
Here is the code :
**with open('stocknews.csv','w') as new_file:
csv_writer=csv.writer(new_file, delimiter=' ')
csv_reader=csv.reader('stocknews.csv')
i=0
lenght=len(soup.find_all('div',{'class':'eachStory'}))**
**for i in range(lenght):
print(i+1,")")
headlines+=[soup.find_all('div',{'class':'eachStory'})[. i].find_all('a')[-1].text]
descriptions+=[soup.find_all('div',{'class':'eachStory'}). [i].find_all('p')[0].text]
print(headlines[i])
print(descriptions[i])**
**i+=1
print(i)
for i in csv_reader :
csv_writer.writerow(['headlines','descriptions'])
csv_writer.writerow([headlines, descriptions])**
I'm pretty sure the problem lies within the last few lines. i.e. csv_writer.writerow.. I've tried many things but never managed to save to csv correctly.
This isn't really an answer (There are a lot of things I don't quite understand about your code and I can't test it without any data to work with).
However,
for i in csv_reader :
csv_writer.writerow(['headlines','descriptions'])
csv_writer.writerow([headlines, descriptions])**
This is a loop over a csv_reader but what are you actually looping over? Normally you don't loop over csv_reader (I have never seen this actual construction before). Normally you loop over some collection - such as the lines in a text file which you have read. As far as I can tell, you are looping the csv_reader itself. There is only one csv_reader. hence, only one loop.
This would be more typical:
lines = csv_reader.readlines()
for line in lines:
pass #do something
I have no idea why you have double asterisks sprinkled around your code. I would suggest you to break this down and step through each line carefully - is it doing what you think it is doing? Is each intermediate step correct? There isn't a lot of code here, but 1) is the reading of the file working? 2) are the headlines and descriptions being read as you expect? 3) Once you have the headlines and descriptions, are they being written out correctly?
I would like to read in delimited data (length unknown) embedded in a larger .txt file. The usual ways, using np.loadtxt, np.genfromtxt, or pd.read_csv don't seem to work as they throw an error when encountering a bad line. Of course, you can handle bad lines but I haven't found an option to just stop and return the already imported data.
Is there such an option which I overlooked, or do I have to go back and evaluate the file line by line.
Any suggestions would be appreciated :)
Something like this should work, though it might well be better to pre-process the file to fix whatever is causing the issue instead of only reading in data up to that point.
import csv
with open('try.csv', newline='') as csvfile:
rows = []
reader = csv.reader(csvfile)
try:
for row in reader:
rows.append(row)
# You should change Exception to be more specific
except Exception as e:
print("Caught", e)
# These are the rows that could be read
print(rows)
I am pulling lines out of a larger file that meet a specific criteria out and writing them to a new file. I need to replace the first occurrence of ":" with ":|" in each line.
I didn't know I needed to do this when I wrote my first Python (3.8) script to pull the lines which is this successful code:
with open('path/freakinginput.txt', 'r') as i, open('path/freakingoutput.txt', 'w') as o:
for line in i:
if (line.find('RQSTSVCS') != -1):
o.write(line)
As you might expect, this results in a file of all the lines that contain the string RQSTSVCS and it makes a tidy table.
I thought that I could just do:
with open('path/freakinginput.txt', 'r') as i, open('path/freakingoutput.txt', 'w') as o:
for line in i:
if (line.find('RQSTSVCS') != -1):
o.write(line.replace(':',':|',1))
Nothing happens when I do this way but I am at a loss. I have tried different ways of defining the line, for instance:
o.write(line).replace(':',':|',1)
which still doesn't work.
I'm not getting any errors. Python just doesn't write the output.
Please be gentle. I'm pretty darned good at C++ scripting but I'm really new to Python and have been thrown into the deep end of a project. Thank y'all in advance.
y'all!
I don't know why I didn't think of downloading an editor before but I put my code into PyCharm and I was able to figure out the proper placement of the replace() method.
This is the correct code:
with open('path/freakinginput.txt', 'r') as i, open('path/freakingoutput.txt', 'w') as o:
for line in i:
if line.find('RQSTSVCS') != -1:
o.write(line.replace(':', ':|', 1))
I'm awful thankful for your input and I appreciate you!
Tobey
so as i'm out of ideas I've turned to geniuses on this site.
What I want to be able to do is to have two separate csv files. One of which has a bunch of store names on it, and the other to have black listed stores.
I'd like to be able to run a python script that reads the 'black listed' sheet, then checks if those specific names are within the other sheet, and if they are, then delete those off the main sheet.
I've tried for about two days straight and cannot for the life of me get it to work. So i'm coming to you guys to help me out.
Thanks so much in advance.
p.s If you can comment the hell out out of the script so I know what's going on it would be greatly appreciated.
EDIT: I deleted the code I originally had but hopefully this will give you an idea of what I was trying to do. (I also realise it's completely incorrect)
import csv
with open('Black List.csv', 'r') as bl:
reader = csv.reader(bl)
with open('Destinations.csv', 'r') as dest:
readern = csv.reader(dest)
for line in reader:
if line in readern:
with open('Destinations.csv', 'w'):
del(line)
The first thing you need to be aware of is that you can't update the file you are reading. Textfiles (which include .csv files) don't work like that. So you have to read the whole of Destinations.csv into memory, and then write it out again, under a new name, but skipping the rows you don't want. (You can overwrite your input file, but you will very quickly discover that is a bad idea.)
import csv
blacklist_rows = []
with open('Black List.csv', 'r') as bl:
reader = csv.reader(bl)
for line in reader:
blacklist_rows.append(line)
destination_rows = []
with open('Destinations.csv', 'r') as dest:
readern = csv.reader(dest)
for line in readern:
destination_rows.append(line)
Now at this point you need to loop through destination_rows and drop any that match something in blacklist_rows, and write out the rest. I can't suggest what the matching test should look like, because you haven't shown us your input data, so I don't actually know that blacklist_rows and destination_rows contain.
with open('FilteredDestinations.csv', 'w') as output:
writer = csv.writer(output)
for r in destination_rows:
if not r: # trap for blank rows in the input
continue
if r *matches something in blacklist_rows*: # you have to code this
continue
writer.writerow(r)
You could try Pandas
import pandas as pd
df1 = pd.read_csv("Destinations.csv")
df2 = pd.read_csv("Black List.csv")
blacklist = df2["column_name_in_blacklist_file"].tolist()
df3 = df2[~df2['destination_column_name'].isin(blacklist)]
df3.to_csv("results.csv")
print(df3)
There is one answer to this question:
Getting "newline inside string" while reading the csv file in Python?
But this didn't work when I used the accepted answer.
If the answer in the above link doesn't work and you have opened multiple files during the execution of your code, go back and make sure you have closed all your previous files when you were done with them.
I had a script that opened and processed multiple files. Then at the very end, it kept throwing a _csv.Error in the same manner that Amit Pal saw.
My code runs about 500 lines and has three stages where it processes multiple files in succession. Here's the section of code that gave the error. As you can see, the code is plain vanilla:
f = open('file.csv')
fread = csv.reader(f)
for row in fread:
do something
And the error was:
for row in fread:
_csv.Error: newline inside string
So I told the script to print what the row....OK, that's not clear, here's what I did:
print row
f = open('file.csv')
fread = csv.reader(f)
for row in fread:
do something
Interestingly, what printed was the LAST LINE from one of the previous files I had opened and processed.
What made this really weird was that I used different variable names, but apparently the data was stuck in a buffer or memory somewhere.
So I went back and made sure I closed all previously opened files and that solved my problem.
Hope this helps someone.