How to print 'fieldnames' using python csvreader - python

I have a large csv file that I want to work with and the column heading may change over time. The first thing I want to do is see the field names. I can see the csvreader.fieldnames object in the documentation [link], but I can't find any examples.
What's the simplest way to get started?

You can use a csv.DictReader when you already know the columns that the CSV file will have.
If you don't know that then you could consider reading the first line with csv.reader to obtain the names of the columns and then read the file again with a csv.DictReader (which can then be instantiated with the right fieldnames).

Here is an example,
with open('yourFile.csv', "rt", newline='') as csvfile:
csvreader = csv.DictReader(csvfile,
delimiter=',',
quotechar='|')
return csvreader.fieldnames

Related

Replace comma with semicolon when creating Csv Dataframe

I have a code that creates a csv file, when I first open it I everything is in one column so I have to do the usual
Go to Data and do the following. The data is then spplited into columns.
I work with Office 365, and recently I was told that if I change the commas with semicolons then when I open the newly created file Csv file, Excel will automatically open the file already separated into columns.
I’m asking for some advice here, since having to do this process for every created Csv file is really time consuming.
Looking for a way to alter my code so it does this automatically maybe instead of splitting columns with commas, do it with semicolons in this case. Just to try if this works out.
with open('created.csv', 'w', newline='') as f:
writer = csv.writer(f)
[1]: https://i.stack.imgur.com/OtxO4.png
If you already want to transform an existing file you can do it like that:
with open('created.csv', 'r', encoding='utf-8') as f_in, open("outfile.csv", 'w') as f_out:
for line in f_in:
line = line.split(",")
line = ";".join(line)
f_out.write(line)
In case you have already a dataframe you can do it like #jezrael said in the comment with:
df.to_csv('created.csv', sep=';')
As mention in the comment you are already using the csv module to write your file. You have to change this line in your code:
writer = csv.writer(f)
to
writer = csv.writer(f, delimiter=';')
As for me if I open a csv splitted with "," I have to that thing you described in your question. But if I open a csv splitted with ";" it's already in the right columns.
This is (for Windows user at least) dependent on your region settings. This can be different for everyone dependent on your language settings.
You can check them here and also change it if you want:
https://www.itsupportguides.com/knowledge-base/office-2013/excel-20132016-how-to-change-csv-delimiter-character/

CSV reader not reading entire file

I have looked at previous answers to this question, but in each of those scenarios the questioners were asking about something specific they were doing with the file, but the problem occurs for me even when I am not.
I have a .csv file of 27,204 rows. When I open the python interpreter:
python
import csv
o = open('btc_usd1hour.csv','r')
p = csv.reader(o)
for row in p:
print(row)
I then only see roughly the last third of the document displayed to me.
Try so, at me works:
with open(name) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)
reference:
https://docs.python.org/3.6/library/csv.html#csv.DictReader
Try the following code
import csv
fname = 'btc_usd1hour.csv'
with open(fname, newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
It is difficult to tell what is the problem without having the sample. I guess the problem would be removed if you add that newline='' for opening the file.
Use the with construct to close the file automatically. Use the f name for a file object when no further explanation is needed. Store the file name to fname to make future modifications easier (and also for easy copying the code fragment for your later programs).
olisch may be right that the console just scrolled so fast you could not see the result. You can write the result to another text file like this:
with open(fname, newline='') as fin,\
open('output.txt', 'w') as fout:
reader = csv.reader(fin)
for row in reader:
fout.write(repr(row) + '\n')
The repr function converts the row list into its string representation. The print calls that function internally, so you will have the same result that you otherwise observe on screen.
maybe your scrollback buffer is just to short to see the whole list?
In general your csv.reader call should be working fine, except your 27k rows aren't extremly long so that you might be able to hit any 64bit boundaries, which would be quite uncommon.
len(o) might be interesting to see.

CSV to python list of dicts?

I looked through the questions already posted and my problems isn't quite solved. I think this should be fairly straight forward but I am getting tripped up with the variations. Hoping that after getting walked through this one file, then I can upload and parse the rest.
What I am trying to do:
File is open states data (and other files in dropbox): ca_bills.csv
Convert .csv to python: I think it should be converted to a python list of dicts
Use the headers in the file as keys within dicts
I tried this but it didn't do what I wanted + I wonder if there is way to pull the fieldname from the headers of each file
def csv_dict_writer(fp, fieldnames, data):
with open(fp, "wb") as out_file:
writer = csv.DictWriter(out_file, deliminter=',', fieldnames=fieldnames)
writer.writeheader()
for row in data:
writer.writerow(row)
I also did this but this only prints and doesn't write to a file:
with open('ca_bills.csv') as output_file:
reader = csv.reader(output_file)
for row in reader:
print row
Thanks so much! This may be similar to other question but really couldn't extract what I needed. Appreciate your insights.
result=list(csv.DictReader(fp))

How to add a header to a csv file in Python?

I've tried many solutions to add a header to my csv file, but nothing's working properly. Here they are :
I used the writerow method, but my data are overwriting the first row.
I used the DictWriter method, but I don't know how to fill it correctly. Here is my code:
csv = csv.DictWriter(open(directory +'/csv.csv', 'wt'), fieldnames = ["stuff1", "stuff2", "stuff3"], delimiter = ';')
csv.writeheader(["stuff1", "stuff2", "stuff3"])
I got a "2 arguments instead of one" error and I really don't know why.
Any advice?
All you need to do is call DictWriter.writeheader() without arguments:
with open(os.path.join(directory, 'csv.csv'), 'wb') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames = ["stuff1", "stuff2", "stuff3"], delimiter = ';')
writer.writeheader()
You already told DictWriter() what your headers are.
I encountered a similar problem when writing the CSV file.
I had to read the csv file and modify some of the fields in it.
To write the header in the CSV file, i used the following code:
reader = csv.DictReader(open(infile))
headers = reader.fieldnames
with open('ChartData.csv', 'wb') as outcsv:
writer1 = csv.writer(outcsv)
writer1.writerow(headers)
and when you write the data rows, you can use a DictWriter in the following way
writer = csv.DictWriter(open("ChartData.csv", 'a' ), headers)
In the above code "a" stands for appending.
In conclusion - > use a to append data to csv after you have written your header to the same file

Adding quotes and a tab to each element in a csv file

How can I add quotes and a tab to every element in a csv file using python?
For example, I want to make this csv sample:
TitleA,TitleB,TitleC,TitleD,TitleE,....
Data1,Data2,<null>,Data4,<null>,....
DataX,<null>,<null>,DataY,<null>,....
Look like this:
"TitleA" "TitleB" "TitleC" "TitleD" "TitleE" ....
"Data1" "Data2" "<null>" "Data4" "<null>" ....
"DataX" "<null>" "<null>" "DataY" "<null>" ....
I am essentially converting a csv file into a tab separated file where all the elements are enclosed in single quotes...
Is there a quick method to do this?
Any help is appreciated!
What you're doing is converting from one CSV dialect to another, right?
So, you do that by defining two CSV dialects, and creating a reader for one and a writer for the other.
And fortunately, these dialects are both simple enough (the input is even the default) that you don't need to do anything fancy:
with open('in.csv', 'r') as infile, open('out.csv', 'w') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile, delimiter='\t', quoting=csv.QUOTE_ALL)
writer.writerows(reader)
In very old versions of Python, you may have to replace that last line with two:
for line in reader:
writer.writerow(line)
See Dialects and Formatting Parameters for further details.

Categories