[英]Python script to read a text file and write into a csv file
I have a text file in which each row has multiple words (which I want to consider as columns). 我有一个文本文件,其中每一行都有多个单词(我想将其视为列)。 Now I want to read all the data from this text file and create a csv file with rows and columns.
现在,我想从此文本文件中读取所有数据,并创建具有行和列的csv文件。 I am written the code till here -
我写的代码到这里-
import csv
f=open("text.txt", "r")
reader=csv.reader(f)
offile=open("output.csv","wb")
writer=csv.writer(offile,delimiter='\t',quotechar='"',quoting=csv.QUOTE_ALL)
for row in reader:
........
f.close()
offile.close()
I am not able to understand how to divide each row into columns and write this columns and rows back while writing a csv file? 我无法理解如何在编写csv文件时将每一行分成几列,然后再写回这些列和行? I am a newbie to python, so a good example I will be very greatful.
我是python的新手,因此我将是一个很好的例子。
Thanks 谢谢
Try splitting the lines via a regular expression: 尝试通过正则表达式拆分行:
line = "Foo bar baz quux"
import re
pieces = re.split("\s+", line)
print pieces
This results in 这导致
['Foo', 'bar', 'baz', 'quux']
The regular expression used above matches for multiple (+) white space characters (\\s) 上面使用的正则表达式匹配多个(+)空格字符(\\ s)
import re
data = open("test.txt").read()
lines_of_data = data.splitlines()
writer=csv.writer(offile,delimiter='\t',quotechar='"',quoting=csv.QUOTE_ALL)
writer.writerows(map(lambda line:re.split("\s\s\s\s+",line.strip()),lines_of_data))
data = open('test.txt').read()
lines_of_data = data.splitlines()
tmp = []
for i in range(len(lines_of_data)):
tmp.append(lines_of_data[i].split())
data_df = pd.DataFrame(tmp)
data_df.to_csv('test.csv')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.