So I have a csv file called result and I'm getting this:
ID,age,sex,city,province,country
[]
11816.0,44,female,NaN,Aichi
[]
11814.0,64,male,Nagoya City,Aichi
[]
11813.0,64,male,Nagoya City,Aichi
when I just want this:
11816.0,44,female,NaN,Aichi # each line is a list
11814.0,64,male,Nagoya City,Aichi
11813.0,64,male,Nagoya City,Aichi
as in without the empty lists.
How would I go about doing this? I tried to do
with open('result.csv', 'wt', newline='') as fout:
csvout = csv.writer(fout)
csvout.writerows('result.csv')
and I tried this:
with open("filename.csv", "r") as f:
for line in f:
print(line, end="")
and I tried this:
with open('result.csv') as csv_file:
reader = csv.reader(csv_file)
solution = [i] for row in reader]
result = [[items for items in row if items != ''] for row in reader]
but none of them worked.
CodePudding user response:
EDITED (after collecting more information):
I do think that attempting to work around bad data is not worth the trouble, but to answer the question literally would require comething like this:
with open('result.csv') as f:
lines = f.readlines()
for l in filter(lambda x: x.strip() != '[]', lines):
print(l.strip())
this will print the filtered list to stdout; you can then capture that output and save is as a new csv file. In any Unix-based OS you can just redirect the stdout to a file, properly named.
==== ORIGINAL =====
It's difficult to understand what exactly you are trying to do in your code samples. For example, this line:
csvout.writerows('result.csv')
will write every character in the string (which looks like a file name, but who cares) into a file - whatever is attached to the writer object (results.csv in your sample #1 above).
Are you trying to write a collection of objects to a csv file?