我有一个像这种格式的txt文件:
Monday, Jul 24
8:45 apple 1
school
ball
dog
8:50 poirot 2
rouge
chien
chat
8:55 football 3
asroma
juventus
inter
如何将这些值放入csv而忽略第一行(7月24日星期一)并在每行中插入实际日期?我需要这种格式:
A1 Monday, Jul 24
B1:8:45 apple 1 B2:school B3:ball B4:dog B5:ACTUALLY DATE
C1:8:50 poirot 2 C2:rouge C3:chien C4:chat C5:ACTUALLY DATE
etcetc..
所以我想把它们放在这种格式中。 这是我的实际代码:
import urllib2
import unicodecsv as csv
import os
import sys
import io
import time
import datetime
import pandas as pd
from bs4 import BeautifulSoup
import sys
import re
#def to_2d(l,n):
# return [l[i:i+n] for i in range(0, len(l), n)]
with open('air.txt', 'r') as f:
x = f.read()
print x
req_text = x.split('Test')[1: -1]
data = []
for text in req_text:
text = text.split('\n', 1)[1]
for line in text.strip().splitlines():
data.append([line])
#maindatatable = to_2d(data, 4)
with open('output.csv', "wb") as f:
output = csv.writer(f, delimiter=';',quotechar = '"', quoting=csv.QUOTE_NONNUMERIC, encoding='latin-1')
output.writerows(data)
f.close()
我尝试使用to_2d(l,n),但它不起作用,任何想法我怎么能用实际日期达到这种格式? [Python 2.7]
更新了代码,但仍无效:
import urllib2
import unicodecsv as csv
import os
import sys
import io
import time
import datetime
import pandas as pd
from bs4 import BeautifulSoup
import sys
import re
def to_2d(l,n):
return [l[i:i+n] for i in range(0, len(l), n)]
with open('air.txt', 'r') as f:
x = f.read()
print x
req_text = x.split('Load')[1: -1]
data = []
for text in req_text:
text = text.split('\n', 1)[1]
for line in text.strip().splitlines():
data.append([line])
#maindatatable = to_2d(data, 4)
from string import ascii_uppercase as LETTERS
with open('air.txt') as f, open('output.csv', 'wb') as g:
actual_date = f.readline().strip()
while True:
first_line = f.readline().strip()
if not first_line:
break
second_line = f.readline().strip()
third_line = f.readline().strip()
fourth_line = f.readline().strip()
the_time, noun, number = first_line.split(' ')
number = int(number)
letter = LETTERS[number]
new_line = '%s1:%s %s %s %s2:%s %s3:%s %s4:%s %s5:%s' % (letter, the_time, noun, number, letter, second_line, letter, third_line, letter, fourth_line, letter, actual_date)
print (new_line)
f.close()

答案 0 :(得分:0)
此脚本只打印输出行而不是将它们写入输出csv文件。但是,这对您来说很容易修改。另请注意,(a)最多可以用这种方式写入25行,因为只有25个字母来自' B'到' Z' (b)如果您的示例输出文件中的第一行要写入csv文件,那么它将无法输入到pandas。
该脚本的工作原理是读取第一行并将其内容保存为@Input() get arrayValue(value: string | number[]) {
if (typeof value == 'string') {
this._arrayValue = JSON.parse(value)
} else {
this._arrayValue = value
}
}
,以便在输出行中使用。然后它一次读取四行,用于创建单个输出行。
actual_date