我有一个要从文本文件'arrowhead.txt'中读取的列表,它是字符串。我想读取字符串,然后将其再次写入python中的另一个文本文档。我知道我可以将文本从输入文件复制到目标文件,但是我需要使用python。有什么帮助吗?输入文件:
arrowhead
BEGIN
28,85
110,80
118,80
127,80
135,80
141,80
147,80
152,80
156,80
160,80
162,80
164,80
165,80
165,80
END
BEGIN
139,38
183,81
186,85
188,86
189,88
190,90
191,92
183,93
180,95
177,96
174,97
170,100
166,102
162,105
157,107
151,110
145,113
140,116
135,118
130,121
126,125
122,125
119,127
117,130
115,130
113,130
112,132
112,132
END
输出文件应采用相同的格式。需要帮助!
这应该将数据从arrowhead.txt
复制到output.txt
。测试一下,看看它是否适合您想要的工作。
with open('arrowhead.txt', 'r') as read_file:
with open('output.txt', 'w') as out_file:
for row in read_file:
out_file.write(row)
使用Pandas完成此任务要容易得多,它将在文件名'test_copy.csv'中创建文本的相同副本。这是代码:
import pandas as pd
df = pd.read_csv('test.csv')
df.to_csv('test_copy.csv', index=False)
注意:如果您未安装熊猫,则可以使用pip install pandas进行安装