Read and write small or large CSV/TXT files in a simple manner
pip install quick-csv
Example 1: read and write csv or txt files
fromquickcsv.fileimport*# read a csv filelist_model=read_csv('data/test.csv')
foridx,modelinenumerate(list_model):
print(model)
list_model[idx]['id']=idx# save a csv filewrite_csv('data/test1.csv',list_model)
# write a text filewrite_text('data/text1.txt',"Hello World!")
# read a text fileprint(read_text('data/text1.txt'))Example 2: create dataframe from a list of models
fromquickcsv.fileimport*# read a csv filelist_model=read_csv('data/test.csv')
# create a dataframe from list_modeldf=create_df(list_model)
# printprint(df)Example 1: read large csv file
fromquickcsv.largefileimport*if__name__=="__main__":
csv_path=r"umls_atui_rels.csv"# a large file (>500 MB)total_count=0defprocess_partition(part_df,i):
print(f"Part {i}")
defprocess_row(row,i):
globaltotal_countprint(i)
total_count+=1list_results=read_large_csv(csv_file=csv_path,row_func=process_row,partition_func=process_partition)
print("Return: ")
print(list_results)
print("Total Record Num: ",total_count)Example 2: query from a large csv file
fromquickcsv.largefileimport*if__name__=="__main__":
csv_path=r"umls_sui_nodes.csv"# a large file (>500 MB)total_count=0# process each partition in the large filedefprocess_partition(part_df,i):
print(f"Part {i}")
print()
# process each row in a partition while readingdefprocess_row(row,i):
globaltotal_countprint(row)
total_count+=1# field is a field in the csv file, and value is the value you need to find within the csv filelist_results=read_large_csv(csv_file=csv_path, field="SUI",value="S0000004", append_row=True, row_func=process_row,partition_func=process_partition)
print("Return: ")
print(list_results)
print("Total Record Num: ",total_count)Example 3: read top N records from the large csv file
fromquickcsv.largefileimport*if__name__=="__main__":
csv_path=r"umls_atui_rels.csv"total_count=0# return top 10 rows in the csv filelist_results=read_large_csv(csv_file=csv_path,head_num=10)
print("Return: ")
print(list_results)
print("Total Record Num: ",total_count)The quick-csv project is provided by Donghua Chen.