python - 我正在尝试从文本文件中读取数据并将该数据写入 csv 文件
问题描述
#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 单元格中获取每一个。
解决方案
推荐阅读
- javascript - 在javascript中动态单击按钮时如何增加值?
- mysql - LIMIT值来源于Mysql中的子查询时如何只查看表的一部分
- spring - Spring webclient连接由远程服务器重置,但不是RestTemplate
- python - 我应该将特征选择与一种热编码一起使用吗?
- sails.js - 如何从 Swagger OpenAPI 规范生成 Sails.js 模型和控制器?
- tensorflow - 使用 tf.layers.dense 和创建我们自己的层之间的区别
- mariadb - 在 Mariadb 中恢复表的表空间错误
- regex - 正则表达式 - 如何根据字符串长度拒绝?
- javascript - 为什么“foreach”会在交互之间的 discord.js 中产生延迟
- rust - Serde:如何实现多个自定义序列?