0

To start...Python noob...

My first goal is to read the first row of a CSV and output. The following code does that nicely.

import csv
csvfile = open('some.csv','rb')
csvFileArray = []
for row in csv.reader(csvfile, delimiter = ','):
    csvFileArray.append(row)
print(csvFileArray[0])

Output looks like...

['Date', 'Time', 'CPU001 User%', 'CPU001 Sys%',......

My second and third tasks deal with formatting.

Thus, if I want the print(csvFileArray[0]) output to contain 'double quotes' for the delimiter how best can I handle that?

I'd like to see...

["Date","Time", "CPU001 User%", "CPU001 Sys%",......

I have played with formatting the csvFileArray field and all I can get it to do is to prefix or append data.

I have also looked into the 'dialect', 'quoting', etc., but am just all over the place.

My last task is to add text into each value (into the array).

Example:

["Test Date","New Time", "Red CPU001 User%", "Blue CPU001 Sys%",......

I've researched a number of methods to do this but am awash in the multiple ways.

Should I ditch the Array as this is too constraining?

Looking for direction not necessarily someone to write it for me.

Thanks.

OK.....refined the code a bit and am looking for direction, not direct solution (need to learn).

import csv
with open('ba200952fd69 - Copy.csv', newline='') as csvfile:
  reader = csv.reader(csvfile)
  for row in reader:
    print (row)
    break

The code nicely reads the first line of the CSV and outputs the first row as follows:

['Date', 'Time', 'CPU001 User%', 'CPU001 Sys%',....

If I want to add formatting to each/any item within that row, would I be performing those actions within the quotes of the print command? Example: If I wanted each item to have double-quotes, or have a prefix of 'XXXX', etc.

I have read through examples of .join type commands, etc., and am sure that there are much easier ways to format print output than I'm aware of.

Again, looking for direction, not immediate solutions.

6
  • provide outputs that you obtain in the three steps Commented Oct 27, 2017 at 18:16
  • what have you tried ? Please ass code so people can guide you. Commented Oct 27, 2017 at 18:17
  • About your second task, it's a bit unclear what you want. Do you want to wrap the strings in your list between double quotes, or do you want to see double quotes when you do print(csvFileArray[0]) ? Commented Oct 27, 2017 at 18:25
  • I would like to output double quotes when I perform the print(csvFileArray[0]). Commented Oct 27, 2017 at 18:28
  • It looks like you actually want to convert your list to JSON. Is this what you want ? If it is, then you can do (based on Sandeep's answer) json.dumps(words). Don't forget to import json before. Commented Oct 27, 2017 at 18:36

1 Answer 1

1

For your first task, I'd recommend using the next function to grab the first row rather than iterating through the whole csv. Also, it might be useful to take a look at with blocks as they are the standard way of dealing with opening and closing files.

For your second question, it looks like you want to change the format of the print statement. Note that it is printing strings, which is indicated by the single quotes around each element in the array. This has nothing to do with the csv module, but simply because you are print an array of strings. To print with double quotes, you would have to reformat the print statement. You could take a look at this for some ways on doing that.

For your last question, I'd recommend looking at list comprehensions. E.g.,

["Test " + word for word in words].

If words = ["word1", "word2"], then this would return ["Test word1", "Test word2"].

Edit: If you want to add a different value to each value in the array, you could do something similar. Let prefixes be an array of prefixes you want to add to the word in words at the same index location. You could then use the list comprehension:

[prefix + " " + word for prefix, word in zip(prefixes, words)]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.