简体   繁体   中英

New variable required? How to write to new file (csv?) in python 3.x?

I am working with statistics in a library environment - and recording some monthly numbers (fairly new to programming and python). Using an opportunity to learn some python where the process in the old ILS was read in perl.

The scenario is:

a. I have an output from an oracle report that will be saved as a csv file b. That file is opened and lines are split so the strings are hashable. c. Using indexes, those strings are iterated, counted, and, using a dictionary, meanings are attached. My output using print() is exactly what I need it to be (the results are simple and easily readable by everyone).

But I am having a hard time turning this output into writable lines in a new file.

for stat in dataSet:
statCount = stat_reader.count(stat)
name = team[stat[:3]]
code = codes[stat[-3:]]

    #for line in sorted(m_stats):
print(name + ': ' + code + ': ' + str(statCount))

I have tried input(''), csv.writer, csv.DictWriter, and other things in order to move that perfectly fine and printable output to a usable variable for writing as printed to a new file...

What am I missing (or am I over-thinking it?)

Also, I am expecting to output in .csv, but would be happy to output in .odt or .docx

::

Slight bit of follow-up

Running the exact same fixed script from yesterday (data is clean, all the key/value pairs are in the dictionary, etc) - but today, on another month's data, ONLY the FINAL line of output to the .csv file.

In addition, each time I run the script, I get zero errors in either cdm or pycharm and the output is a different result.

In addition (2.0), These results I describe here are the same in different directories (running from either cmd or pycharm)

This strikes me as odd. Any ideas?

import csv
with open('out.csv', 'wb') as csvfile:
   writer = csv. writer(csvfile, delimiter=':')
   #for stat in dataSet: # I guess that was your point - if so uncomment this line and add indent to line below
   writer.writerow([name, code, str(statCount)])

may be helpful to read: https://docs.python.org/3/library/csv.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM