To complement the previous answers, I whipped up a quick class to write to CSV files. It makes it easier to manage and close open files and achieve consistency and cleaner code if you have to deal with multiple files.
Is there a built-in way to use read_csv to read only the first n lines of a file without knowing the length of the lines ahead of time? I have a large file that takes a long time to read, and occasionally only want to use the first, say, 20 lines to get a sample of it (and prefer not to load the full thing and take the head of it).
If I knew the total number of lines I could do something like footer_lines = total_lines - n and pass this to the skipfooter keyword arg. My current solution is to manually grab the first n lines with python and StringIO it to pandas:
import pandas as pd
from StringIO import StringIO
n = 20
with open('big_file.csv', 'r') as f:
head = ''.join(f.readlines(n))
df = pd.read_csv(StringIO(head))
It’s not that bad, but is there a more concise, ‘pandasic’ (?) way to do it with keywords or something?
nrows : int, default NoneNumber of rows of file to read.Usefulfor reading pieces of large files
这似乎有效。使用标准大型测试文件之一(988504479字节,5344499行):
In[1]:import pandas as pd
In[2]: time z = pd.read_csv("P00000001-ALL.csv", nrows=20)
CPU times: user 0.00 s, sys:0.00 s, total:0.00 s
Wall time:0.00 s
In[3]: len(z)Out[3]:20In[4]: time z = pd.read_csv("P00000001-ALL.csv")
CPU times: user 27.63 s, sys:1.92 s, total:29.55 s
Wall time:30.23 s
I think you can use the nrows parameter. From the docs:
nrows : int, default None
Number of rows of file to read. Useful for reading pieces of large files
which seems to work. Using one of the standard large test files (988504479 bytes, 5344499 lines):
In [1]: import pandas as pd
In [2]: time z = pd.read_csv("P00000001-ALL.csv", nrows=20)
CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
Wall time: 0.00 s
In [3]: len(z)
Out[3]: 20
In [4]: time z = pd.read_csv("P00000001-ALL.csv")
CPU times: user 27.63 s, sys: 1.92 s, total: 29.55 s
Wall time: 30.23 s
One,Two,Three
a,1,one
b,2,two
,3,three
d,4,nan
e,5,five
nan,6,
g,7,seven
>>> pandas.read_csv('test.csv', na_values={'One': [], "Three": []})
One Two Three
0 a 1 one
1 b 2 two
2 NaN 3 three
3 d 4 nan
4 e 5 five
5 nan 6 NaN
6 g 7 seven
I’m using the pandas library to read in some CSV data. In my data, certain columns contain strings. The string "nan" is a possible value, as is an empty string. I managed to get pandas to read “nan” as a string, but I can’t figure out how to get it not to read an empty value as NaN. Here’s sample data and output
One,Two,Three
a,1,one
b,2,two
,3,three
d,4,nan
e,5,five
nan,6,
g,7,seven
>>> pandas.read_csv('test.csv', na_values={'One': [], "Three": []})
One Two Three
0 a 1 one
1 b 2 two
2 NaN 3 three
3 d 4 nan
4 e 5 five
5 nan 6 NaN
6 g 7 seven
It correctly reads “nan” as the string “nan’, but still reads the empty cells as NaN. I tried passing in str in the converters argument to read_csv (with converters={'One': str})), but it still reads the empty cells as NaN.
I realize I can fill the values after reading, with fillna, but is there really no way to tell pandas that an empty cell in a particular CSV column should be read as an empty string instead of NaN?
Python’s built-in CSV module can handle this easily:
import csv
with open("output.csv", "wb") as f:
writer = csv.writer(f)
writer.writerows(a)
This assumes your list is defined as a, as it is in your question. You can tweak the exact format of the output CSV via the various optional parameters to csv.writer() as documented in the library reference page linked above.
Update for Python 3
import csv
with open("out.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(a)
回答 1
您可以使用pandas:
In[1]:import pandas as pdIn[2]: a =[[1.2,'abc',3],[1.2,'werew',4],[1.4,'qew',2]]In[3]: my_df = pd.DataFrame(a)In[4]: my_df.to_csv('my_csv.csv', index=False, header=False)
In [1]: import pandas as pd
In [2]: a = [[1.2,'abc',3],[1.2,'werew',4],[1.4,'qew',2]]
In [3]: my_df = pd.DataFrame(a)
In [4]: my_df.to_csv('my_csv.csv', index=False, header=False)
回答 2
import csvwith open(file_path,'a')as outcsv:#configure writer to write standard csv file
writer = csv.writer(outcsv, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
writer.writerow(['number','text','number'])for item in list:#Write item to outcsv
writer.writerow([item[0], item[1], item[2]])
import csv
with open(file_path, 'a') as outcsv:
#configure writer to write standard csv file
writer = csv.writer(outcsv, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
writer.writerow(['number', 'text', 'number'])
for item in list:
#Write item to outcsv
writer.writerow([item[0], item[1], item[2]])
If for whatever reason you wanted to do it manually (without using a module like csv,pandas,numpy etc.):
with open('myfile.csv','w') as f:
for sublist in mylist:
for item in sublist:
f.write(item + ',')
f.write('\n')
Of course, rolling your own version can be error-prone and inefficient … that’s usually why there’s a module for that. But sometimes writing your own can help you understand how they work, and sometimes it’s just easier.
yourlist =[[...],...,[...]]
columns =["abcd","bcde","cdef"]#a csv with 3 columns
index =[i[0]for i in yourlist]#first element of every list in yourlist
not_index_list =[i[1:]for i in yourlist]
pd = pandas.DataFrame(not_index_list, columns = columns, index = index)#Now you have a csv with columns and index:
pd.to_csv("mylist.csv")
The good part you can change somethings to make a better csv file:
yourlist = [[...],...,[...]]
columns = ["abcd","bcde","cdef"] #a csv with 3 columns
index = [i[0] for i in yourlist] #first element of every list in yourlist
not_index_list = [i[1:] for i in yourlist]
pd = pandas.DataFrame(not_index_list, columns = columns, index = index)
#Now you have a csv with columns and index:
pd.to_csv("mylist.csv")
with open('csvfile','a')as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')for i in range(0, len(data)):
spamwriter.writerow(data[i])
Make sure to indicate lineterinator='\n' when create the writer; otherwise, an extra empty line might be written into file after each data line when data sources are from other csv file…
Here is my solution:
with open('csvfile', 'a') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
for i in range(0, len(data)):
spamwriter.writerow(data[i])
I am reading a csv file into pandas. This csv file constists of four columns and some rows, but does not have a header row, which I want to add. I have been trying the following:
In python 3 things are a little different, but way simpler and less error prone. It’s a good idea to tell the CSV your file should be opened with utf8 encoding, as it makes that data more portable to others (assuming you aren’t using a more restrictive encoding, like latin1)
import csv
toCSV = [{'name':'bob','age':25,'weight':200},
{'name':'jim','age':31,'weight':180}]
with open('people.csv', 'w', encoding='utf8', newline='') as output_file:
fc = csv.DictWriter(output_file,
fieldnames=toCSV[0].keys(),
)
fc.writeheader()
fc.writerows(toCSV)
Note that csv in python 3 needs the newline='' parameter, otherwise you get blank lines in your CSV when opening in excel/opencalc.
Alternatively: I prefer use to the csv handler in the pandas module. I find it is more tolerant of encoding issues, and pandas will automatically convert string numbers in CSVs into the correct type (int,float,etc) when loading the file.
pandas will take care of opening the file for you if you give it a path, and will default to utf8 in python3, and figure out headers too.
a dataframe is not the same structure as what CSV gives you, so you add one line upon loading to get the same thing: dataframe.to_dict('records')
pandas also makes it much easier to control the order of columns in your csv file. By default, they’re alphabetical, but you can specify the column order. With vanilla csv module, you need to feed it an OrderedDict or they’ll appear in a random order (if working in python < 3.5). See: Preserving column order in Python Pandas DataFrame for more.
import csv
with open('file_name.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(('colum1', 'colum2', 'colum3'))
for key, value in dictionary.items():
writer.writerow([key, value[0], value[1]])
This would be the simplest way to write data to .csv file
Here is another, more general solution assuming you don’t have a list of rows (maybe they don’t fit in memory) or a copy of the headers (maybe the write_csv function is generic):
def gen_rows():
yield OrderedDict(name='bob', age=25, weight=200)
yield OrderedDict(name='jim', age=31, weight=180)
def write_csv():
it = genrows()
first_row = it.next() # __next__ in py3
with open("people.csv", "w") as outfile:
wr = csv.DictWriter(outfile, fieldnames=list(first_row))
wr.writeheader()
wr.writerow(first_row)
wr.writerows(it)
Note: the OrderedDict constructor used here only preserves order in python >3.4. If order is important, use the OrderedDict([('name', 'bob'),('age',25)]) form.
Good guesses for encoding is “ascii” and “utf8”. You can also leave the encoding off, and it will use the system default encoding, which tends to be UTF8, but may be something else.
The reason it is throwing that exception is because you have the argument rb, which opens the file in binary mode. Change that to r, which will by default open the file in text mode.
Your code:
import csv
ifile = open('sample.csv', "rb")
read = csv.reader(ifile)
for row in read :
print (row)
New code:
import csv
ifile = open('sample.csv', "r")
read = csv.reader(ifile)
for row in read :
print (row)
Your problem is you have the b in the open flag.
The flag rt (read, text) is the default, so, using the context manager, simply do this:
with open('sample.csv') as ifile:
read = csv.reader(ifile)
for row in read:
print (row)
The context manager means you don’t need generic error handling (without which you may get stuck with the file open, especially in an interpreter), because it will automatically close the file on an error, or on exiting the context.
import csv
with open('coors.csv', mode='r')as infile:
reader = csv.reader(infile)with open('coors_new.csv', mode='w')as outfile:
writer = csv.writer(outfile)for rows in reader:
k = rows[0]
v = rows[1]
mydict ={k:v for k, v in rows}print(mydict)
当我运行上面的代码时,我得到一个ValueError: too many values to unpack (expected 2)。如何从csv文件创建一个字典?谢谢。
I am trying to create a dictionary from a csv file. The first column of the csv file contains unique keys and the second column contains values. Each row of the csv file represents a unique key, value pair within the dictionary. I tried to use the csv.DictReader and csv.DictWriter classes, but I could only figure out how to generate a new dictionary for each row. I want one dictionary. Here is the code I am trying to use:
import csv
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
for rows in reader:
k = rows[0]
v = rows[1]
mydict = {k:v for k, v in rows}
print(mydict)
When I run the above code I get a ValueError: too many values to unpack (expected 2). How do I create one dictionary from a csv file? Thanks.
回答 0
我相信您正在寻找的语法如下:
import csv
with open('coors.csv', mode='r')as infile:
reader = csv.reader(infile)with open('coors_new.csv', mode='w')as outfile:
writer = csv.writer(outfile)
mydict ={rows[0]:rows[1]for rows in reader}
或者,对于python <= 2.7.1,您需要:
mydict = dict((rows[0],rows[1])for rows in reader)
I believe the syntax you were looking for is as follows:
import csv
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
mydict = {rows[0]:rows[1] for rows in reader}
Alternately, for python <= 2.7.1, you want:
mydict = dict((rows[0],rows[1]) for rows in reader)
I’d suggest adding if rows in case there is an empty line at the end of the file
import csv
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
mydict = dict(row[:2] for row in reader if row)
回答 7
一线解决方案
import pandas as pd
dict ={row[0]: row[1]for _, row in pd.read_csv("file.csv").iterrows()}
If you are OK with using the numpy package, then you can do something like the following:
import numpy as np
lines = np.genfromtxt("coors.csv", delimiter=",", dtype=None)
my_dict = dict()
for i in range(len(lines)):
my_dict[lines[i][0]] = lines[i][1]
with open(csv_file)as f:
csv_list =[[val.strip()for val in r.split(",")]for r in f.readlines()](_,*header),*data = csv_list
csv_dict ={}for row in data:
key,*values = row
csv_dict[key]={key: value for key, value in zip(header, values)}
You can convert it to a Python dictionary using only built-ins
with open(csv_file) as f:
csv_list = [[val.strip() for val in r.split(",")] for r in f.readlines()]
(_, *header), *data = csv_list
csv_dict = {}
for row in data:
key, *values = row
csv_dict[key] = {key: value for key, value in zip(header, values)}
Note: Python dictionaries have unique keys, so if your csv file has duplicate ids you should append each row to a list.
for row in data:
key, *values = row
if key not in csv_dict:
csv_dict[key] = []
csv_dict[key].append({key: value for key, value in zip(header, values)})
回答 10
您可以使用它,这非常酷:
import dataconverters.commas as commas
filename ='test.csv'with open(filename)as f:
records, metadata = commas.parse(f)for row in records:print'this is row in dictionary:'+rowenter code here
import dataconverters.commas as commas
filename = 'test.csv'
with open(filename) as f:
records, metadata = commas.parse(f)
for row in records:
print 'this is row in dictionary:'+rowenter code here
input_file = csv.DictReader(open(path_to_csv_file))
csv_dict ={elem:[]for elem in input_file.fieldnames}for row in input_file:for key in csv_dict.keys():
csv_dict[key].append(row[key])
Many solutions have been posted and I’d like to contribute with mine, which works for a different number of columns in the CSV file.
It creates a dictionary with one key per column, and the value for each key is a list with the elements in such column.
input_file = csv.DictReader(open(path_to_csv_file))
csv_dict = {elem: [] for elem in input_file.fieldnames}
for row in input_file:
for key in csv_dict.keys():
csv_dict[key].append(row[key])
with pandas, it is much easier, for example.
assuming you have the following data as CSV and let’s call it test.txt / test.csv (you know CSV is a sort of text file )
a,b,c,d
1,2,3,4
5,6,7,8
now using pandas
import pandas as pd
df = pd.read_csv("./text.txt")
df_to_doct = df.to_dict()
for each row, it would be
df.to_dict(orient='records')
and that’s it.
回答 13
尝试使用defaultdict和DictReader。
import csv
from collections import defaultdict
my_dict = defaultdict(list)with open('filename.csv','r')as csv_file:
csv_reader = csv.DictReader(csv_file)for line in csv_reader:for key, value in line.items():
my_dict[key].append(value)
import csv
from collections import defaultdict
my_dict = defaultdict(list)
with open('filename.csv', 'r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
for key, value in line.items():
my_dict[key].append(value)
In[37]:
df = pd.DataFrame(np.random.randn(5,3), columns=list('abc'))
pd.read_csv(io.StringIO(df.to_csv()))Out[37]:Unnamed:0 a b c
000.109066-1.112704-0.545209110.4471141.5253410.317252220.5074950.1378630.886283331.4528671.8883631.168101440.901371-0.7048050.088335
与之比较:
In[38]:
pd.read_csv(io.StringIO(df.to_csv(index=False)))Out[38]:
a b c
00.109066-1.112704-0.54520910.4471141.5253410.31725220.5074950.1378630.88628331.4528671.8883631.16810140.901371-0.7048050.088335
您还可以选择read_csv通过传递index_col=0以下内容来判断第一列是索引列:
In[40]:
pd.read_csv(io.StringIO(df.to_csv()), index_col=0)Out[40]:
a b c
00.109066-1.112704-0.54520910.4471141.5253410.31725220.5074950.1378630.88628331.4528671.8883631.16810140.901371-0.7048050.088335
df = pd.DataFrame('x', index=range(5), columns=list('abc'))
df
a b c
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
# Save DataFrame to CSV.
df.to_csv('file.csv')
pd.read_csv('file.csv')Unnamed:0 a b c
00 x x x
11 x x x
22 x x x
33 x x x
44 x x x
# Now try this again, with the extra argument.
pd.read_csv('file.csv', index_col=[0])
a b c
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
df
Unnamed:0 a b c
00 x x x
11 x x x
22 x x x
33 x x x
44 x x x
df.columns
# Index(['Unnamed: 0', 'a', 'b', 'c'], dtype='object')
df.columns.str.match('Unnamed')# array([ True, False, False, False])
df.loc[:,~df.columns.str.match('Unnamed')]
a b c
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
This issue most likely manifests because your CSV was saved along with its RangeIndex (which usually doesn’t have a name). The fix would actually need to be done when saving the DataFrame, but this isn’t always an option.
Avoiding the Problem: read_csv with index_col argument
IMO, the simplest solution would be to read the unnamed column as the index. Specify an index_col=[0] argument to pd.read_csv, this reads in the first column as the index.
df = pd.DataFrame('x', index=range(5), columns=list('abc'))
df
a b c
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
# Save DataFrame to CSV.
df.to_csv('file.csv')
pd.read_csv('file.csv')
Unnamed: 0 a b c
0 0 x x x
1 1 x x x
2 2 x x x
3 3 x x x
4 4 x x x
# Now try this again, with the extra argument.
pd.read_csv('file.csv', index_col=[0])
a b c
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
Note
You could have avoided this in the first place by
using index=False when creating the output CSV, if your DataFrame does not have an index to begin
with.
df.to_csv('file.csv', index=False)
But as mentioned above, this isn’t always an option.
Stopgap Solution: Filtering with str.match
If you cannot modify the code to read/write the CSV file, you can just remove the column by filtering with str.match:
df
Unnamed: 0 a b c
0 0 x x x
1 1 x x x
2 2 x x x
3 3 x x x
4 4 x x x
df.columns
# Index(['Unnamed: 0', 'a', 'b', 'c'], dtype='object')
df.columns.str.match('Unnamed')
# array([ True, False, False, False])
df.loc[:, ~df.columns.str.match('Unnamed')]
a b c
0 x x x
1 x x x
2 x x x
3 x x x
4 x x x
Another case that this might be happening is if your data was improperly written to your csv to have each row end with a comma. This will leave you with an unnamed column Unnamed: x at the end of your data when you try to read it into a df.
I am new to Python. I need to write some data from my program to a spreadsheet. I’ve searched online and there seem to be many packages available (xlwt, XlsXcessive, openpyxl). Others suggest to write to a .csv file (never used CSV and don’t really understand what it is).
The program is very simple. I have two lists (float) and three variables (strings). I don’t know the lengths of the two lists and they probably won’t be the same length.
I want the layout to be as in the picture below:
The pink column will have the values of the first list and the green column will have the values of the second list.
So what’s the best way to do this?
P.S. I am running Windows 7 but I won’t necessarily have Office installed on the computers running this program.
I wrote this using all your suggestions. It gets the job done but it can be slightly improved.
How do I format the cells created in the for loop (list1 values) as scientific or number?
I do not want to truncate the values. The actual values used in the program would have around 10 digits after the decimal.
回答 0
import xlwt
def output(filename, sheet, list1, list2, x, y, z):
book = xlwt.Workbook()
sh = book.add_sheet(sheet)
variables =[x, y, z]
x_desc ='Display'
y_desc ='Dominance'
z_desc ='Test'
desc =[x_desc, y_desc, z_desc]
col1_name ='Stimulus Time'
col2_name ='Reaction Time'#You may need to group the variables together#for n, (v_desc, v) in enumerate(zip(desc, variables)):for n, v_desc, v in enumerate(zip(desc, variables)):
sh.write(n,0, v_desc)
sh.write(n,1, v)
n+=1
sh.write(n,0, col1_name)
sh.write(n,1, col2_name)for m, e1 in enumerate(list1, n+1):
sh.write(m,0, e1)for m, e2 in enumerate(list2, n+1):
sh.write(m,1, e2)
book.save(filename)
import xlwt
def output(filename, sheet, list1, list2, x, y, z):
book = xlwt.Workbook()
sh = book.add_sheet(sheet)
variables = [x, y, z]
x_desc = 'Display'
y_desc = 'Dominance'
z_desc = 'Test'
desc = [x_desc, y_desc, z_desc]
col1_name = 'Stimulus Time'
col2_name = 'Reaction Time'
#You may need to group the variables together
#for n, (v_desc, v) in enumerate(zip(desc, variables)):
for n, v_desc, v in enumerate(zip(desc, variables)):
sh.write(n, 0, v_desc)
sh.write(n, 1, v)
n+=1
sh.write(n, 0, col1_name)
sh.write(n, 1, col2_name)
for m, e1 in enumerate(list1, n+1):
sh.write(m, 0, e1)
for m, e2 in enumerate(list2, n+1):
sh.write(m, 1, e2)
book.save(filename)
Use DataFrame.to_excel from pandas. Pandas allows you to represent your data in functionally rich datastructures and will let you read in excel files as well.
You will first have to convert your data into a DataFrame and then save it into an excel file like so:
In [1]: from pandas import DataFrame
In [2]: l1 = [1,2,3,4]
In [3]: l2 = [1,2,3,4]
In [3]: df = DataFrame({'Stimulus Time': l1, 'Reaction Time': l2})
In [4]: df
Out[4]:
Reaction Time Stimulus Time
0 1 1
1 2 2
2 3 3
3 4 4
In [5]: df.to_excel('test.xlsx', sheet_name='sheet1', index=False)
and the excel file that comes out looks like this:
Note that both lists need to be of equal length else pandas will complain. To solve this, replace all missing values with None.
xlrd/xlwt (standard): Python does not have this functionality in it’s standard library, but I think of xlrd/xlwt as the “standard” way to read and write excel files. It is fairly easy to make a workbook, add sheets, write data/formulas, and format cells. If you need all of these things, you may have the most success with this library. I think you could choose openpyxl instead and it would be quite similar, but I have not used it.
Tablib (powerful, intuitive): Tablib is a more powerful yet intuitive library for working with tabular data. It can write excel workbooks with multiple sheets as well as other formats, such as csv, json, and yaml. If you don’t need formatted cells (like background color), you will do yourself a favor to use this library, which will get you farther in the long run.
csv (easy): Files on your computer are either text or binary. Text files are just characters, including special ones like newlines and tabs, and can be easily opened anywhere (e.g. notepad, your web browser, or Office products). A csv file is a text file that is formatted in a certain way: each line is a list of values, separated by commas. Python programs can easily read and write text, so a csv file is the easiest and fastest way to export data from your python program into excel (or another python program).
Excel files are binary and require special libraries that know the file format, which is why you need an additional library for python, or a special program like Microsoft Excel, Gnumeric, or LibreOffice, to read/write them.
import xlwt
style = xlwt.XFStyle()
style.num_format_str = '0.00E+00'
...
for i,n in enumerate(list1):
sheet1.write(i, 0, n, fmt)
I surveyed a few Excel modules for Python, and found openpyxl to be the best.
The free book Automate the Boring Stuff with Python has a chapter on openpyxl with more details or you can check the Read the Docs site. You won’t need Office or Excel installed in order to use openpyxl.
CSV stands for comma separated values. CSV is like a text file and can be created simply by adding the .CSV extension
for example write this code:
f = open('example.csv','w')
f.write("display,variable x")
f.close()
you can open this file with excel.
回答 5
import xlsxwriter
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()# Widen the first column to make the text clearer.
worksheet.set_column('A:A',20)# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold':True})# Write some simple text.
worksheet.write('A1','Hello')# Text with formatting.
worksheet.write('A2','World', bold)# Write some numbers, with row/column notation.
worksheet.write(2,0,123)
worksheet.write(3,0,123.456)# Insert an image.
worksheet.insert_image('B5','logo.png')
workbook.close()
import xlsxwriter
# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)
# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})
# Write some simple text.
worksheet.write('A1', 'Hello')
# Text with formatting.
worksheet.write('A2', 'World', bold)
# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)
# Insert an image.
worksheet.insert_image('B5', 'logo.png')
workbook.close()
The xlsxwriter library is great for creating .xlsx files. The following snippet generates an .xlsx file from a list of dicts while stating the order and the displayed names:
💡 Note 1 – I’m purposely not answering to the exact case the OP presented. Instead, I’m presenting a more generic solution IMHO most visitors seek. This question’s title is well-indexed in search engines and tracks lots of traffic
💡 Note 2 – If you’re not using Python3.6 or newer, consider using OrderedDict in headers. Before Python3.6 the order in dict was not preserved.
The easiest way to import the exact numbers is to add a decimal after the numbers in your l1 and l2. Python interprets this decimal point as instructions from you to include the exact number. If you need to restrict it to some decimal place, you should be able to create a print command that limits the output, something simple like:
print variable_example[:13]
Would restrict it to the tenth decimal place, assuming your data has two integers left of the decimal.
#!/usr/local/bin/python3import pyoo
# Connect to LibreOffice using a named pipe # (named in the soffice process startup)
desktop = pyoo.Desktop(pipe='oo_pyuno')
wkbk = desktop.open_spreadsheet("<xls_file_name>")
sheet = wkbk.sheets['Sheet1']# Write value 'foo' to cell E5 on Sheet1
sheet[4,4].value='foo'
wkbk.save()
wkbk.close()
If your need is to modify an existing workbook, the safest way would be to use pyoo. You need to have some libraries installed and it takes a few hoops to jump through but once its set up, this would be bulletproof as you are leveraging the wide and solid API’s of LibreOffice / OpenOffice.
Please see my Gist on how to set up a linux system and do some basic coding using pyoo.
Here is an example of the code:
#!/usr/local/bin/python3
import pyoo
# Connect to LibreOffice using a named pipe
# (named in the soffice process startup)
desktop = pyoo.Desktop(pipe='oo_pyuno')
wkbk = desktop.open_spreadsheet("<xls_file_name>")
sheet = wkbk.sheets['Sheet1']
# Write value 'foo' to cell E5 on Sheet1
sheet[4,4].value='foo'
wkbk.save()
wkbk.close()