[英]how to create file txt filename from file csv with python 2.7
import csv
with open("HORSES.csv") as f:
for row in csv.reader(f):
# Number of pages plus one
for horse in row:
print (horse)
with open("%s.txt" % horse, "w") as f:
f.close()
此內容文件horses.csvwest,美國,亞洲,黑色。 輸出OS可以,但是對於僅創建.txt文件的black.txt文件horses.csv的末尾,而不是從列表pd csv文件創建文件txt的情況下,如何從列表文件horses.csv中使用文件名創建.txt文件,例如west.txt usa.txt asia.txt black.txt
您的last with
語句不是循環的一部分,因此您只會獲得存儲在horse
的最后一個值。
如果您需要多個.txt文件,請將它們放入一個循環並一次創建一個:
import csv
Horse_types = []
with open("HORSES.csv") as f:
for row in csv.reader(f):
for horse in row:
print (horse)
Horse_types.append(horse)
for ht in Horse_types:
with open("%s.txt" % ht, "w") as f:
# More stuff here?
f.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.