txt,csv,json互相轉化


也沒啥,記下來怕忘了.說明都在代碼里面:

麻蛋,這個着色好難看

import csv
import json

#從txt變為csv
student_txt=[];
with open("student.txt",mode='r',encoding='utf-8')as student_txt_file_name:
	for i in student_txt_file_name.readlines():
		student_txt.append(i.strip('\n').split(" "));	#去掉換行符,轉為list,使用空格分開(因為txt里面是以空格隔開)
with open("student.csv",mode='w',encoding='ansi',newline='')as student_csv_file_name:	#newline='':作用是防止空行產生;encoding='ansi'創建一個以ASCII編碼的csv文件,用utf-8的話我的Excel不認,亂的
	write=csv.writer(student_csv_file_name);	#創建一個編輯對象
	for i in student_txt:
		write.writerow(i);	#把每一個列表(子列表)作為行寫入
#我沒有主動關閉這兩個文件的原因我不說,反正我知道,我自己忘了就讓我自己想去.

#從csv變為txt
student_csv=[];
student_txt=[];
with open("student.csv",mode='r',encoding='ansi')as student_csv_file_name:#編碼讀寫一致
	read_object=csv.reader(student_csv_file_name);
	with open("student1.txt",mode='w',encoding='ansi')as student_txt_file_name:
		for i in read_object:
			j=' '.join(i)+'\n';	#這種奇怪的轉化方式亮瞎我的眼.還一閃一閃的像迪廳!,
			student_txt_file_name.writelines(j);
#不一定非得是列表,字典也可以
#txt轉json
student_json=[];
student_txt=[];
with open('student.txt',mode='r',encoding='utf-8')as student_txt_file_name:
	with open("student.json",mode='w',encoding='ansi')as student_json_file_name:
		for i in student_txt_file_name.readlines():
			student_txt.append(i.strip('\n').split(' '));
		key=student_txt[0];#作為鍵
		for i in range(1,len(student_txt)):
			student_json_temp=[];
			for j in zip(key,student_txt[i]):		#zip接受多個可迭代對象作為參數,然后將這些對象中的對應位置的元素組成一個個的元組:zip([1,2,3],[4,5,6])返回[(1,4),(2,5),(3,6)]
				k=":".join(j);		#這個的作用就是把(1,4)變成"1:4"
				student_json_temp.append(k);
			student_json.append(student_json_temp);
		json.dump(student_json,student_json_file_name);#這種寫方式讓我有些郁悶,總覺得像創建對象似的
#json轉txt
student_json=[];
student_txt=[];
with open('student_json轉txt.txt',mode='w',encoding='ansi')as student_txt_file_name:
	with open("student.json",mode='r',encoding='ansi')as student_json_file_name:
		read_object=json.load(student_json_file_name);
		for i in read_object:
			head_list=[];
			body_list=[];
			for j in i:
				k=j.split(':');
				if len(student_json)==0:
					head_list.append(k[0]);
				body_list.append(k[1]);
			if len(student_json)==0:
				student_txt_file_name.write(' '.join(head_list)+'\n');
				student_json.append(student_json);	#用了一次就沒用了
			student_txt_file_name.write(' '.join(body_list)+'\n');

#csv轉json
student_csv=[];
student_json=[];
with open("student.csv",mode='r',encoding='ansi')as student_csv_file_name:
	read_object=csv.reader(student_csv_file_name);	#用csv模塊自帶的函數來完成讀寫操作
	with open("student_csv轉json.json",mode='w',encoding='ansi')as student_json_file_name:
		for i in read_object:
			student_csv.append(i);
		key=student_csv[0];
		for i in range(1,len(student_csv)):
			student_json_temp=[];
			for j in zip(key,student_csv[i]):
				k=":".join(j);
				student_json_temp.append(k);
			student_json.append(student_json_temp);
		json.dump(student_json,student_json_file_name);

#json轉csv
student_csv=[];
student_json=[];
with open("student.json",mode='r',encoding='ansi')as student_json_file_name:
	with open("student_json轉csv.csv",mode='w',encoding='ansi',newline='')as student_csv_file_name:
		read_object=json.load(student_json_file_name);
		write=csv.writer(student_csv_file_name);
		for i in read_object:	#讀出來是列表
			ledlist=[];
			templist=[];
			for a in i:
				j=a.split(':');
				ledlist.append(j[0]);
				templist.append(j[1]);
			if len(student_csv)==0:
				student_csv.append(ledlist);
			student_csv.append(templist);
		for i in student_csv:
			write.writerow(i);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM