1 format()
形如str.format()
,format中的内容用于代替str中的 {} 和
:
当读取的文件较大需要分开存储时可用此函数对其编号
1 >>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
2 ‘hello world‘
3
4 >>> "{0} {1}".format("hello", "world") # 设置指定位置
5 ‘hello world‘
6
7 >>> "{1} {0} {1}".format("hello", "world") # 设置指定位置
8 ‘world hello world‘
参考:https://blog.csdn.net/zhchs2012/article/details/84328742
2 存储文件时默认存储在当前程序所在的目录下,也可用 .//newfilewalk 表示存储在当前目录的newfilewalk下
若没有newfilewalk文件夹会报错,要提前新建
data_xls.to_csv(‘.\\chengji\\化学.csv‘, encoding=‘utf-8‘)
3 若在一个程序中后面要读前面存储下的文件,此时前面用to_csv()存储时要加上index=False,此时便不会存储索引列,否则在程序后面容易出现列数不对应的错误
4 to_csv()函数
参数: mode: 默认是‘w‘,即写数据,当想要将数据追加写入文件后面时用mode=‘a+‘,这个时候一定要注意header=None,否则会将列名也写入文件中,后续处理很容易出错,出错了只能用二分法找了.
app_actived_train = pd.read_csv(‘./labelencoder_file/app_actived_train.csv‘, iterator=True) pieceID = 1 loop = True while loop: try: df = app_actived_train.get_chunk(100000) # 10万 df.columns = [‘index‘, ‘uid‘, ‘appid‘] df1 = df[‘uid‘] df1 = pd.DataFrame(df1) df = df[‘appid‘].str.split(‘#‘, expand=True).stack().reset_index(level=1, drop=True).rename(‘appid‘) df = {‘index‘: df.index, ‘appid‘: df.values} df = pd.DataFrame(df) df = pd.merge(df1, df, left_index=True, right_on=‘index‘, how=‘left‘) df.to_csv(‘./labelencoder_file/app_actived_train.csv‘, mode=‘a+‘, index=False, header=None) print(pieceID * 100000) pieceID += 1 del df, df1 except StopIteration: loop = False print(‘imps_log process finish!‘)
原文:https://www.cnblogs.com/xxswkl/p/10993325.html