[英]Print strings with line break Python
import csv
import datetime
with open('soundTransit1_remote_rawMeasurements_15m.txt','r') as infile, open('soundTransit1.txt','w') as outfile:
inr = csv.reader(infile,delimiter='\t')
#ouw = csv.writer(outfile,delimiter=' ')
for row in inr:
d = datetime.datetime.strptime(row[0],'%Y-%m-%d %H:%M:%S')
s = 1
p = int(row[5])
nr = [format(s,'02')+format(d.year,'04')+format(d.month,'02')+format(d.day,'02')+format(d.hour,'02')+format(d.minute,'02')+format(int(p*0.2),'04')]
outfile.writelines(nr+'/n')
Using the above script, I have read in a .txt file and reformatted it as 'nr' so it looks like this: 使用上面的脚本,我读了一个.txt文件并将其重新格式化为'nr',因此它看起来像这样:
['012015072314000000']
['012015072313450000']
['012015072313300000']
['012015072313150000']
['012015072313000000']
['012015072312450000']
['012015072312300000']
['012015072312150000']
..etc. ..等等。
I need to now print it onto my new .txt file, but Python is not allowing me to print 'nr' with line breaks after each entry, I think because the data is in strings. 现在,我需要将其打印到新的.txt文件中,但是Python不允许我在每次输入后用换行符打印'nr',我认为这是因为数据是字符串形式的。 I get this error: 我收到此错误:
TypeError: can only concatenate list (not "str") to list
Is there another way to do this? 还有另一种方法吗?
You are trying to combine a list with a string, which cannot work. 您正在尝试将列表与字符串组合在一起,但无法使用。 Simply don't create a list in nr
. 只是不要在nr
创建列表。
import csv
import datetime
with open('soundTransit1_remote_rawMeasurements_15m.txt','r') as infile, open('soundTransit1.txt','w') as outfile:
inr = csv.reader(infile,delimiter='\t')
#ouw = csv.writer(outfile,delimiter=' ')
for row in inr:
d = datetime.datetime.strptime(row[0],'%Y-%m-%d %H:%M:%S')
s = 1
p = int(row[5])
nr = "{:02d}{:%Y%m%d%H%M}{:04d}\n".format(s,d,int(p*0.2))
outfile.write(nr)
There is no need to put your string into a list; 无需将您的字符串放入列表中。 just use outfile.write()
here and build a string without a list: 只需在此处使用outfile.write()
并构建不包含列表的字符串即可:
nr = format(s,'02') + format(d.year,'04') + format(d.month, '02') + format(d.day, '02') + format(d.hour, '02') + format(d.minute, '02') + format(int(p*0.2), '04')
outfile.write(nr + '\n')
Rather than use 7 separate format()
calls, use str.format()
: 与其使用7个单独的format()
调用, str.format()
使用str.format()
:
nr = '{:02}{:%Y%m%d%H%M}{:04}\n'.format(s, d, int(p * 0.2))
outfile.write(nr)
Note that I formatted the datetime
object with one formatting operation, and I included the newline into the string format. 请注意,我使用一种格式化操作对datetime
对象进行了格式化,并将换行符包含在字符串格式中。
You appear to have hard-coded the s
value; 您似乎已经硬编码了s
值; you may as well put that into the format directly: 您也可以直接将其输入格式:
nr = '01{:%Y%m%d%H%M}{:04}\n'.format(d, int(p * 0.2))
outfile.write(nr)
Together, that updates your script to: 一起,将您的脚本更新为:
with open('soundTransit1_remote_rawMeasurements_15m.txt', 'r') as infile,\
open('soundTransit1.txt','w') as outfile:
inr = csv.reader(infile, delimiter='\t')
for row in inr:
d = datetime.datetime.strptime(row[0], '%Y-%m-%d %H:%M:%S')
p = int(int(row[5]) * 0.2)
nr = '01{:%Y%m%d%H%M}{:04}\n'.format(d, p)
outfile.write(nr)
Take into account that the csv
module works better if you follow the guidelines about opening files; 请注意,如果您遵循有关打开文件的准则,则csv
模块会更好地工作。 in Python 2 you need to open the file in binary mode ( 'rb'
), in Python 3 you need to set the newline
parameter to ''
. 在Python 2中,您需要以二进制模式( 'rb'
)打开文件,在Python 3中,您需要将newline
参数设置为''
。 That way the module can control newlines correctly and supports including newlines in column values. 这样,模块可以正确控制换行符,并支持在列值中包括换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.