我正在尝试从文本文件中读取数据并将该数据写入 csv 文件

时间:2021-03-15 20:15:36

标签: python etl

#first import the csv module
import csv

#open the text file in read mode
text_file = open("randomaddresses.txt","r")
for line in text_file:
    line.rstrip("\n")
    

#open a csv file in write mode
csv_file = open("randomaddresses.csv", "w")
reader = csv.reader(text_file, delimiter= ",")
writer = csv.writer(csv_file)
writer.writerow(["Address","City","State","Zip Code"])
for row in reader:
    writer.writerow(row)


#close the two files
text_file.close()
csv_file.close()

我试图让我的代码在 csv 文件中显示输出,但是当我打开它时,我得到的只是我的第一行,但没有显示任何地址。

the data in the text file is this:
371 Arch Street
Cranford, NJ 07016
679 Race Street
Lockport, NY 14094
579 7th Avenue
Santa Clara, CA 95050
12 West Street
Orange Park, FL 32065
906 2nd Street West
Great Falls, MT 59404
851 Locust Lane
Lombard, IL 60148
837 Fawn Court
Centereach, NY 11720
343 Jefferson Court
Norwalk, CT 06851
34 3rd Street North
Athens, GA 30605
143 10th Street
North Wales, PA 19454

我想在地址、城市、州和邮编标题下的 excel 单元格中获取每个内容。

0 个答案:

没有答案