简体   繁体   English

在python3中读取和写入表格文本文件

[英]reading and writing tabular text files in python3

I have several tabular text files in a folder.The data in the first column are strings,second and third column integers.How can I read them all in and write the first column and the second column edited(say divided by two) into a new tabular text file? 我在一个文件夹中有几个表格文本文件。第一列中的数据是字符串,第二列和第三列整数。如何将它们全部读取,并将第一列和第二列编辑后的内容(例如被二除)写入一个新的表格文本文件? I've just begun learning python so please kindly give example code if possible.Thanks. 我刚刚开始学习python,因此请尽可能给出示例代码。

This snippet might give you some idea 该代码段可能会给您一些想法

# terrible code for educational purposes only!

in_stream = open(filename_in)
out_stream = open(filename_out, 'w')
output = csv.writer(out_stream)

for col_string, col_value1, col_value2 in csv.reader(in_stream):
    new_row = [col_string, col_value1 / 2, col_value2]
    output.write(new_row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM