我在编写CSV文件时遇到问题。每次我使用python编写CSV文件时,突然Here is the CSV file with blank space的每一行都有空白。因此,我在互联网上寻找解决方案。然后某个网站说我只需要将“ w”更改为“ wb”,但是在尝试运行该程序后,就会发生错误。
错误:TypeError:需要一个类似字节的对象,而不是'str'
我的代码:
seat =[]
csvfile = open('coba.csv')
seating = csv.reader(csvfile)
for line in seating:
seat.append(line)
print("Buy seat ?")
answer_1 = input("Answer : ")
if (answer_1 == "yes"):
answer_2 = input("Enter preferred seat: ")
if (answer_2 == "A1"):
row = 1
column = 0
seat[row][column] = "X"
writer = csv.writer(open('coba.csv', 'wb'))
writer.writerows(seat)
for line in seat:
print(' | '.join(line))
CSV文件:
[Business]
A1,A2,A3,A4,A5
B1,B2,B3,B4,B5
[Economy]
C1,C2,C3,C4,C5
D1,D2,D3,D4,D5
答案 0 :(得分:0)
您正在读“ r”,正在写“ wb”
您要么更改每个元素的数据类型,要么简单地
将wb
更改为w
,就可以了
完整代码
import csv
seat =[]
csvfile = open('coba.csv')
seating = csv.reader(csvfile)
for line in seating:
seat.append(line)
print("Buy seat ?")
answer_1 = input("Answer : ")
if (answer_1 == "yes"):
answer_2 = input("Enter preferred seat: ")
if (answer_2 == "A1"):
row = 1
column = 0
seat[row][column] = "X"
writer = csv.writer(open('coba.csv', 'w'))
writer.writerows(seat)
for line in seat:
print(' | '.join(line))
输出
Buy seat ?
Answer : yes
Enter preferred seat: A1
[Business]
X | A2 | A3 | A4 | A5
B1 | B2 | B3 | B4 | B5
[Economy]
C1 | C2 | C3 | C4 | C5
D1 | D2 | D3 | D4 | D5
在excel中打开生成的文件
答案 1 :(得分:0)
在您的import { WINDOW } from '@ng-toolkit/universal';
import { Component, OnInit , Inject} from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
constructor(
@Inject(WINDOW) private window: Window,
) { }
ngOnInit() {
this.startCarousel();
}
startCarousel() {
this.sectionTwoInterval = setInterval(() => {
this.next(1);
}, 5000);
this.sectionFourInterval = setInterval(() => {
this.next(2);
}, 5000);
}
goToSlide(carousel: number, slide: number) {
switch (carousel) {
case 1:
clearInterval(this.sectionTwoInterval);
this.sectionTwoActive = slide;
break;
case 2:
clearInterval(this.sectionFourInterval);
this.sectionFourActive = slide;
break;
}
}
next(carousel: number, stop?: boolean) {
if (stop === true) { this.stopInterval(carousel); }
switch (carousel) {
case 1:
if (this.sectionTwoActive !== this.sectionTwoImages.length - 1) {
this.sectionTwoActive++;
} else {
this.sectionTwoActive = 0;
}
break;
case 2:
if (this.sectionFourActive !== this.sectionFourImages.length - 1) {
this.sectionFourActive++;
} else {
this.sectionFourActive = 0;
}
break;
}
}
back(carousel: number, stop?: boolean) {
if (stop === true) { this.stopInterval(carousel); }
switch (carousel) {
case 1:
if (this.sectionTwoActive !== 0) {
this.sectionTwoActive--;
} else {
this.sectionTwoActive = this.sectionTwoImages.length - 1;
}
break;
case 2:
if (this.sectionFourActive !== 0) {
this.sectionFourActive--;
} else {
this.sectionFourActive = this.sectionFourImages.length - 1;
}
break;
}
}
stopInterval(carousel: number) {
switch (carousel) {
case 1:
clearInterval(this.sectionTwoInterval);
break;
case 2:
clearInterval(this.sectionFourInterval);
break;
}
}
调用中使用newline
关键字参数,
open()