python:写入txt文件

分类: python |
#
One
f
= open('a.txt', 'w')
f.write('Hello
world!')
f.close()
#
Two
with
open('a.txt', 'w') as f:
|
import
numpy as tf
a_1 =
np.arange(5)
a_2, a_3 = a_1 * 2, a_1
* 3
np.savetxt('a.txt',
(a_1, a_2, a_3))
|
DataFrame.to_csv(path_or_buf=None, sep=', ', na_rep='', float_format=None, columns=None,header=True, index=True, index_label=None, mode='w', encoding=None, compression=None, quoting=None, quotechar='"', line_terminator='\n', chunksize=None, tupleize_cols=None, date_format=None, doublequote=True, escapechar=None, |
path_or_buf=None:
路径或对象,如果没有提供,结果将返回为字符串。
sep
na_rep
:
float_format
:
columns
header
:
index
index_label
:
如果需要,可以使用索引列的列标签。如果没有给出,且标题和索引为True,则使用索引名称。如果数据文件使用多索引,则应该使用这个序列。如果值为False,不打印索引字段。在R中使用index_label=False
更容易导入索引.
mode
:
encoding
:
表示在输出文件中使用的编码的字符串,Python 2上默认为“ASCII”和Python 3上默认为“UTF-8”。
compression
:
表示在输出文件中使用的压缩的字符串,允许值为“gzip”、“bz2”、“xz”,仅在第一个参数是文件名时使用。
line_terminator
:字符串,默认为 ‘\n’
在输出文件中使用的换行字符或字符序列
quoting
:
默认值为to_csv.QUOTE_MINIMAL。如果设置了浮点格式,那么浮点将转换为字符串,因此csv.QUOTE_NONNUMERIC会将它们视为非数值的。
quotechar
:
用于引用字段的字符
doublequote
:布尔,默认为Ture
控制一个字段内的quotechar
escapechar
:
在适当的时候用来转义sep和quotechar的字符
chunksize
:
tupleize_cols
:
从版本0.21.0中删除:此参数将被删除,并且总是将多索引的每行写入CSV文件中的单独行
(如果值为false)将多索引列作为元组列表(如果TRUE)或以新的、扩展的格式写入,其中每个多索引列是CSV中的一行。
date_format
:
字符串对象转换为日期时间对象
decimal:
字符识别为小数点分隔符。例如。欧洲数据使用 ’,’
time_stamp_1 = pd.Series(time_stamp_tmp, name='Timestamp')#name='Timestamp'注意:
img_name_time_2 = pd.Series(img_name_tmp, name='ImageName')
head = ['Timestamp', 'ImageName']
save = pd.DataFrame({'Timestamp':time_stamp_1, 'ImageName':img_name_time_2}, columns=head)
save.to_csv('data_time_imgname.txt', index=False, sep='\t')
with
open('a.txt',
|