my csv program is
studentid,firstname,midterm,final,total,grade
20135447,Delta,47.00,37.00,65.00,DC
20144521,Jeffrey,36.00,22.00,27.60,FF
l tried this code
with open('marks.csv')as file:
line=csv.reader(file)
mydict={rows[0]:rows[1:] for rows in line}
print(mydict)
l got the following traceback error
Traceback (most recent call last):
File "", line 3, in
File "", line 3, in
IndexError: list index out of range
but my desired output is
{20135447:['Delta','47.00','37.00','65.00','DC'], '20144521':['Jeffrey','36.00','22.00','27.60','FF']}
please help me
import csv
mydic = dict()
with open('marks.csv') as myfile:
line = csv.reader(myfile)
for row in line:
mydic[row[0]] = row[1:]
Related
I'm trying to make a script that downloads videos from youtube, but I keep getting the below error.
>>> from pytube import YouTube
>>> vid = YouTube("https://www.youtube.com/watch?v=dfnCAmr569k")
Traceback (most recent call last):
File "<stdin>", line1, in <module>
File "/home/user/.local/lib/python3.8/site-packages/pytube/__main__.py", line 92 in __init__
self.descramble()
File "/home/user/.local/lib/python3.8/site-packages/pytube/__main__.py", line 140 in descramble
apply_signature(self.player_confing_args, fmt, self.js)
File "/home/user/.local/lib/python3.8/site-packages/pytube/extract.py", line 225 in apply_signature
cipher = Cipher(js=js)
File "/home/user/.local/lib/python3.8/site-packages/pytube/cipher.py", line 31 in __init__ var, _ =
self.transform_plan[0].split(".")
ValueError: too many values to unpack (expected 2)
It's may "cipher.py" file's parsing fnc error. Just copy & paste bug fixed source to your "pytube/cipher.py".
related thread : https://github.com/nficano/pytube/issues/641#issuecomment-665268989
So I copied and pasted a demo program from the book I am using to learn Python:
#!/usr/bin/env python
import csv
total = 0
priciest = ('',0,0,0)
r = csv.reader(open('purchases.csv'))
for row in r:
cost = float(row[1]) * float(row[2])
total += cost
if cost == priciest[3]:
priciest = row + [cost]
print("You spent", total)
print("Your priciest purchase was", priciest[1], priciest[0], "at a total cost of", priciest[3])
And I get the Error:
Traceback (most recent call last):
File "purchases.py", line 2, in <module>
import csv
File "/Users/Solomon/Desktop/Python/csv.py", line 5, in <module>
r = csv.read(open('purchases.csv'))
AttributeError: 'module' object has no attribute 'read'
Why is this happening? How do I fix it?
Update:
Fixed All The Errors
Now I'm getting:
Traceback (most recent call last):
File "purchases.py", line 6, in <module>
for row in r:
_csv.Error: line contains NULL byte
What was happening in terms of the CSV.py:
I had a file with the same code named csv.py, saved in the same directory. I thought that the fact that it was named csv .py was screwing it up, so I started a new file called purchases.py, but forgot to delete csv
Don't name your file csv.py.
When you do, Python will look in your file for the csv code instead of the standard library csv module.
Edit: to include the important note in the comment: if there's a csv.pyc file left over in that directory, you'll have to delete that. that is Python bytecode which would be used in place of re-running your csv.py file.
There is a discrepancy between the code in the traceback of your error:
r = csv.read(open('purchases.csv'))
And the code you posted:
r = csv.reader(open('purchases.csv'))
So which are you using?
At any rate, fix that indentation error in line 2:
#!/usr/bin/env python
import csv
total = 0
And create your csv reader object with a context handler, so as not to leave the file handle open:
with open('purchases.csv') as f:
r = csv.reader(f)
I'm trying to save some data scraped using selenium to CSV file using pandas, but I'm getting this error and I don't know what's the problem.
I tried to use header=False instead of the header=['','','',''],
it works without error but gave me an empty CSV file.
Error:
full_datas = list(zip(d_links, d_title, d_price, d_num))
df = pd.DataFrame(data=full_datas)
df.to_csv('cc.csv', encoding='utf-8-sig', index=False, header=['Links','Title,','Price','PNum'])
Empty csv:
full_datas = list(zip(d_links, d_title, d_price, d_num))
df = pd.DataFrame(data=full_datas)
df.to_csv('cc.csv', encoding='utf-8-sig', index=False, header=False)
I'm expecting a CSV file with my data on it
Traceback (most recent call last):
File "E:/data/some/urls.py", line 90, in <module>
df.to_csv('cc.csv', encoding='utf-8-sig', index=False, header=['Links','Title,','Price','PNum'])
File "C:\Users\Iwillsolo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\generic.py", line 3228, in to_csv
formatter.save()
File "C:\Users\Iwillsolo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\formats\csvs.py", line 202, in save
self._save()
File "C:\Users\Iwillsolo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\formats\csvs.py", line 310, in _save
self._save_header()
File "C:\Users\Iwillsolo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\formats\csvs.py", line 242, in _save_header
"aliases".format(ncols=len(cols), nalias=len(header))
ValueError: Writing 0 cols but got 4 aliases
sometimes the list might be empty so i managed to fix it by using zip_longest
thank you guys for trying to help :)
I have a large matrix (15000 rows x 2500 columns) stored using PyTables and getting see how to iterate over the columns of a row. In the documentation I only see how to access each row by name manually.
I have columns like:
ID
X20160730_Day10_123a_2
X20160730_Day10_123b_1
X20160730_Day10_123b_2
The ID column value is a string like '10692.RFX7' but all other cell values are floats. This selection works and I can iterate the rows of results but I cannot see how to iterate over the columns and check their values:
from tables import *
import numpy
def main():
h5file = open_file('carlo_seth.h5', mode='r', title='Three-file test')
table = h5file.root.expression.readout
condition = '(ID == b"10692.RFX7")'
for row in table.where(condition):
print(row['ID'].decode())
for col in row.fetch_all_fields():
print("{0}\t{1}".format(col, row[col]))
h5file.close()
if __name__ == '__main__':
main()
If I just iterate with "for col in row" nothing happens. As the code is above, I get a stack:
10692.RFX7
Traceback (most recent call last):
File "tables/tableextension.pyx", line 1497, in tables.tableextension.Row.__getitem__ (tables/tableextension.c:17226)
KeyError: b'10692.RFX7'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "tables/tableextension.pyx", line 126, in tables.tableextension.get_nested_field_cache (tables/tableextension.c:2532)
KeyError: b'10692.RFX7'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./read_carlo_pytable.py", line 31, in <module>
main()
File "./read_carlo_pytable.py", line 25, in main
print("{0}\t{1}".format(col, row[col]))
File "tables/tableextension.pyx", line 1501, in tables.tableextension.Row.__getitem__ (tables/tableextension.c:17286)
File "tables/tableextension.pyx", line 133, in tables.tableextension.get_nested_field_cache (tables/tableextension.c:2651)
File "tables/utilsextension.pyx", line 927, in tables.utilsextension.get_nested_field (tables/utilsextension.c:8707)
AttributeError: 'numpy.bytes_' object has no attribute 'encode'
Closing remaining open files:carlo_seth.h5...done
You can access a column value by name in each row:
for row in table:
print(row["10692.RFX7"])
Iterate over all columns:
names = table.coldescrs.keys()
for row in table:
for name in names:
print(name, row[name])
I have a problem with the following code:
inputf = open('test.dat', 'r')
lines = inputf.readlines()
rico_clus_e = []
for line in lines:
line.split()
print line
if (line[0] != '#'):
rico_clus_e.append(float(line[4]))
inputf.close()
My test.dat file is:
# specn rico_all rico_all_e rico_clus rico_clus_e rico_farclust rico_far_e extin
a119 1.07038692 0.11109547 0.61473431 0.15063627 0.32590239 0.14777812 0.207
And this gives the following output in my terminal:
# specn rico_all rico_all_e rico_clus rico_clus_e rico_farclust rico_far_e extin
a119 1.07038692 0.11109547 0.61473431 0.15063627 0.32590239 0.14777812 0.207
Traceback (most recent call last):
File "test.py", line 8, in <module>
rico_clus_e.append(float(line[4]))
ValueError: could not convert string to float:
I'm quite confused by this. It had nothing to do with spaces, I checked them all. And if you change 4 by 1, 2 or 3 this works, so it must have something to do with the test.dat file, but I can't seem to figure out how. I'm using python 2.7.3.
line.split() on its own does nothing to line. You must store the result of the method call and use that instead.