pandas write and read csv file

1. Use Pandas to write the list data into a csv file, encapsulate the specific operation into a function, and then call it directly.

def insert_csv(datas):
    df = pd.DataFrame(datas)
    file_name = 'hello'
    df.to_csv('{}.csv'.format(file_name),encoding = "GBK")

      Note: It is possible to write into the csv file, and the encoding error can not be written. You can modify the encoding: change to gb18030 encoding.

               If the data field to be inserted is uncertain, you can insert it by appending, and you must add a parameter modal = "a"#获取csv

          df.to_csv("my.csv", mode='a',header=False)

2. pandas 读取的表格内容
df = pd.read_csv('xxy_yang.csv', encoding="gbk")
lines = list()
#read_result:输出是一个索引,然后根据索引查出数据
read_result = df.reset_index().T.to_dict()
for _index in read_result:

#获取每行的数据
  lines.append(read_result[_index])
# 多进程加多线线程封装写入表格
from concurrent.futures import ThreadPoolExecutor, as_completed
from multiprocessing import Process, Manager
# 返回的manager对象控制了一个server进程,此进程包含的python对象可以被其他的进程通过proxies来访问。从而达到多进程间数据通信且安全
def thread_run(_tuple):
   line_list = _tuple
   #开启进程,利用的是ThreadPoolExecutor,开启100个线程
   executor = ThreadPoolExecutor(max_workers=100)
   all_tasks = [executor.submit(do_data_process, line) for line in line_list]
   data_list = list()
   for future in as_completed(all_tasks):
   result_dict = future.result()
   if result_dict:
     data_list.append(result_dict)

   #这边可以插入数据

   df = pd.DataFRame(data_list)

   df.to_csv("xx.csv")
def do_data_process():
  #该功能是数据处理好后返回数据

  pass

# 创建进程6个进程
if __name__  == '__main__':

data_list = []

pool = list()

with Manager() as mess:

  # 创建进程6个进程

   for start_num in range(6):

     p = Process(target=tread_run, args=((data_list[start_num::6]),))

     p.start()

     pool.append(p)

    for p in pool:

      p.join()

 

 

 

 

Guess you like

Origin blog.csdn.net/xxy_yang/article/details/81221056