python - Why won't my file print? I keep getting an I/O error -
print ('test.csv') # This is where all the data will be in the CSV format import CSV fp Open ('test.csv', 'wb') as the #csv = csv.DictWriter (fp, ['f1', 'f2']) test_file = 'test.csv' csvfile = 'test.csv' Field name = ['some field'] test_file = open ('test.csv', 'wb') csvwriter = csvfile = csv.DictReader (test_file, 'rb'), delimiter = ',', quotechar = '' '' Csv syntax (test_file, delimiter = ',', fieldnames = fieldnames) csvwriter.writerow (for fp in field names dict ((fp, fp)) for line in test_file: csvwriter.writerow (line) test_file.close () line In line: csv.writerow (line) When I run this code IOError is found: The file is not open to read on "for line in test_file", what's the problem here? I like I open it up Given.
At the beginning of the file, you are opening trial .CCV In write mode It will erase what it wrote. Also, you are opening it in binary format, but if it's a csv , you want to open it in text format. Other issues:
with open ('test.csv', 'wb') FP: fp is not used anywhere else in addition to useless in this program, here is useless. csvfile = 'test.csv' csvfile = csv.DictReader (open (test_file, 'rb'), delimiter = ',', quotechar = '' '')) < / pre> The first line is nothing, because it will just overwrite. Again, you are opening blank file.
test_file = open ('test.csv', 'wb') csvwriter = csv.DictWriter (test_file, delimiter = ',', field name = field name) And here you can only file again.
for line in line: csv.writerow (line) What do you want to do here? You get your Iterator in a very confusing way. Perhaps you want to do something like such as the line in test_file . In addition, the author has a method of DictWriter , not CSV's. This will take an exception.
Comments
Post a Comment