Note that there are some explanatory texts on larger screens.

plurals
  1. POextra characters in csv output
    text
    copied!<p>I'm trying to write a Python program to convert a simple text file to a.csv file.</p> <p>Each line of input contains names after <code>from:</code> followed by a name. Lines that do not start with <code>from:</code> are to be ignored.</p> <p>Input:</p> <pre><code>from: Lance Cummins This line is ignored by the program from: Jackie Cohen Hello world from: Chris Paul Lalala from: Jackie Cohen Message </code></pre> <p>The output of the program should be a CSV file, showing the name of the person followed by the number of times they appeared in the input file:</p> <pre><code>Lance Cummins,1 Chris Paul,1 Jackie Cohen,2 </code></pre> <p>However, the actual output of the program is this:</p> <pre><code>["Chris Paul": 1, "Lance Cummins": 1, "Jackie Cohen": 2} </code></pre> <p><strong>What confuses me is that I had another person run my program on their computer and the result was correct. Why is this happening?</strong> </p> <p>Here is my actual program:</p> <pre><code>def is_field(field_name, s): if s[:len(field_name)] == field_name: return True else: return False def contributor_counts(file_name): fname = open(file_name, "r" ) counts = {} for x in fname: if is_field("from: ", x): x = x.strip("from: ") x = x.rstrip() if x in counts: counts[x] = counts[x] + 1 else: counts[x] = 1 return counts def print_contributors(counts): for x in counts: if counts[x] &gt; 1: print str(x) + " posted " + str(counts[x]) + " times" else: print str(x) + " posted once" def save_contributors(counts, output_file_name): f = open(output_file_name, "w") for value in counts: number = counts[value] y = str(value) + "," + str(number) f.write(y + "\n") f.close() contributions = contributor_counts("long182feed.txt") print_contributors(contributions) save_contributors(contributions, 'contributors.csv') </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload