I’ve recently written a few python scripts that count the number of words in a csv file and then print as output two columns: “Prefix” and “Count” where prefix is the first word in a name in a row and count is the sum of all occurences of that name.
My question is that if I try to output to a .csv file directly, everything gets put in one column and it doesn’t look nice. However, if I output first to a .txt file, and then open that file in excel such that it asks me to make a space delimiter, it looks correct. I bet the answer is pretty basic, but I’m just wondering if there is a simpler way to do this than saving as a .txt and then re-opening as a .csv. Additionally, why can I open a new excel file and open the .txt file from there and it works, whereas if I try to just “open with” the .txt file directly into excel it does not work (and instead gives me the old result with everything in one column)?
Much thanks for any help you can provide!
EDIT:
Output looks like the following when I open the .txt file as a csv with the “open with” button:
Word Count
the 333
Family 54
When I open a new excel file and open the .txt file from there and use the delimiter options that come up before the file opens, it looks correctly like this:
Word, Count
the, 333
Family, 54
CSV = comma separated values
You need to output the data separated by comma and not space. You can save this as .csv and double-click to open in excel. Then the data appears in two columns.
Same reason. If you open a .txt file in excel, it will just open as text. It doesn’t know how to interpret this data (what goes into which columns). When you “open a new excel file and open the .txt file from there”, you are “importing” the data and you tell excel to interpret the data as two columns with space as delimiter, so it works.
You can also open the .txt file directly in excel using “open with” and then use the “Text to Columns” feature to remodel the data in a spreadsheet-friendly way.