Python 使用csv的固定长度文本文件
我有一个csv文件,如下所示:Python 使用csv的固定长度文本文件,python,csv,text,Python,Csv,Text,我有一个csv文件,如下所示: 123456,456789,12345,123.45,123456 123456,456789,12345,123.45,123456 123456,456789,12345,123.45,123456 line = ''.ljust(2).join(row) 123456-----456789-----12345-----123.45-----123456 spacing = ['\t', ' ', ' ', ' ', '\r\
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
line = ''.ljust(2).join(row)
123456-----456789-----12345-----123.45-----123456
spacing = ['\t', ' ', ' ', ' ', '\r\n']
# ... the same code from before ...
line = ''.join([j for i in zip(row, spacing) for j in i])
# ... rest of the code ...
for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, ' ') ...]
for j in i: # now i == (item1, '\t')
j # so j is just the items of each tuple
我对Python编程非常陌生,但我正在学习并发现Python非常有用。我基本上希望输出如下所示:
123456 456789 12345 123.45 123456
123456 456789 12345 123.45 123456
123456 456789 12345 123.45 123456
基本上,所有字段都是右对齐的,具有固定长度。csv文件中没有标题
以下是我迄今为止尝试过的代码,正如我所说,我对Python非常陌生:
import csv
with open('test.csv') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
print(', '.join(row))
with open('test2.txt', 'wb') as f:
writer = csv.writer(f)
writer.writerows(f)
任何帮助都将不胜感激:提前感谢。好的,您的代码有一大堆问题:
import csv
res = []
# start a loop to collect the data
with open('test.csv') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
line = '\t'.join(row) + '\r\n' # the \n is for linebreaks. \r is so notepad loves you too
res.append(line)
# now, outside the loop, we can do this:
with open('test2.txt', 'wb') as f:
f.writelines(res)
编辑
如果要控制间距,可以使用如下函数:
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
line = ''.ljust(2).join(row)
123456-----456789-----12345-----123.45-----123456
spacing = ['\t', ' ', ' ', ' ', '\r\n']
# ... the same code from before ...
line = ''.join([j for i in zip(row, spacing) for j in i])
# ... rest of the code ...
for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, ' ') ...]
for j in i: # now i == (item1, '\t')
j # so j is just the items of each tuple
这将确保每个项目之间有2个空格。默认为空格,但如果要指定ljust将使用的内容,可以向其中添加第二个参数:
line = ''.ljust(5, '-').join(row)
那么每一行看起来都是这样的:
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
line = ''.ljust(2).join(row)
123456-----456789-----12345-----123.45-----123456
spacing = ['\t', ' ', ' ', ' ', '\r\n']
# ... the same code from before ...
line = ''.join([j for i in zip(row, spacing) for j in i])
# ... rest of the code ...
for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, ' ') ...]
for j in i: # now i == (item1, '\t')
j # so j is just the items of each tuple
感谢Philippe T.在评论中提到这一点
第二次编辑
如果希望每个列具有不同的长度,则需要预定义它。最好的方法是创建一个与csv文件列长度相同的列表,每个项都是该列的长度,最后一项是该行的结尾(这很方便,因为''join本身不这样做),然后用行压缩它。假设您希望第一列有一个选项卡,然后在其他列之间各有两个空格。那么您的代码将如下所示:
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
line = ''.ljust(2).join(row)
123456-----456789-----12345-----123.45-----123456
spacing = ['\t', ' ', ' ', ' ', '\r\n']
# ... the same code from before ...
line = ''.join([j for i in zip(row, spacing) for j in i])
# ... rest of the code ...
for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, ' ') ...]
for j in i: # now i == (item1, '\t')
j # so j is just the items of each tuple
列表理解循环有点复杂,但请这样思考:
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
123456,456789,12345,123.45,123456
line = ''.ljust(2).join(row)
123456-----456789-----12345-----123.45-----123456
spacing = ['\t', ' ', ' ', ' ', '\r\n']
# ... the same code from before ...
line = ''.join([j for i in zip(row, spacing) for j in i])
# ... rest of the code ...
for i in zip(row, spacing): # the zip here equals ==> [(item1, '\t'), (item2, ' ') ...]
for j in i: # now i == (item1, '\t')
j # so j is just the items of each tuple
通过列表理解,这将输出:[item1',\t',item2',,…]。你把它连在一起就可以了。试试这个:
import csv
with open('data.csv') as fin, open('out.txt','w') as fout:
data = csv.reader(fin,delimiter=',')
resl = csv.writer(fout,delimiter='\t')
resl.writerows(data)
在制表符分隔的文件中?检查这个问题是如此。。。此外,我认为可以使用BaseStringLjust(或rjust)方法来实现固定长度,而不是使用制表法。若要将每个列定义为不同的长度,您将如何编写?