Python—读取、写入和附加到文件
我是python新手。文件中有不同的端口号。我想遍历端口号。端口之间用逗号分隔。最后,我想把我的端口号附加到这个文件中。我写的代码不起作用,因为结尾总是有换行符。我怎样才能解决这个问题。还有更好的解决办法吗。这是我的密码-Python—读取、写入和附加到文件,python,file,file-io,python-3.x,Python,File,File Io,Python 3.x,我是python新手。文件中有不同的端口号。我想遍历端口号。端口之间用逗号分隔。最后,我想把我的端口号附加到这个文件中。我写的代码不起作用,因为结尾总是有换行符。我怎样才能解决这个问题。还有更好的解决办法吗。这是我的密码- f = open("ports.txt", "r") line = f.readline() line = line.split(",") print(line) if len(line) &g
f = open("ports.txt", "r")
line = f.readline()
line = line.split(",")
print(line)
if len(line) > 0:
del line[-1]
for port in line:
print(port)
f = open("ports.txt", "a")
m = str(self.myPort)+","
f.write(m)
f.close()
在处理逗号分隔的值时,通常应使用 下面的代码应该是不言自明的
import csv
# By using the with statement, you don't have to worry about closing the file
# for reading/writing. This is taken care of automaticly.
with open('ports.txt') as in_file:
# Create a csv reader object from the file object. This will yield the
# next row every time you call next(reader)
reader = csv.reader(in_file)
# Put the next(reader) statement inside a try ... except block. If the
# exception StopIteratorion is raised, there is no data in the file, and
# an IOError is raised.
try:
# Use list comprehension to convert all strings to integers. This
# will make sure no leading/trailing whitespace or any newline
# character is printed to the file
ports = [int(port) for port in next(reader)]
except StopIteration:
raise IOError('No data in file!')
with open('ports.txt', 'wb') as out_file:
# Create a csv writer object
writer = csv.writer(out_file)
# Append your port to the list of ports...
ports.append(self.myPort)
# ...and write the data to the csv file
writer.writerow(ports)
在处理逗号分隔的值时,通常应使用 下面的代码应该是不言自明的
import csv
# By using the with statement, you don't have to worry about closing the file
# for reading/writing. This is taken care of automaticly.
with open('ports.txt') as in_file:
# Create a csv reader object from the file object. This will yield the
# next row every time you call next(reader)
reader = csv.reader(in_file)
# Put the next(reader) statement inside a try ... except block. If the
# exception StopIteratorion is raised, there is no data in the file, and
# an IOError is raised.
try:
# Use list comprehension to convert all strings to integers. This
# will make sure no leading/trailing whitespace or any newline
# character is printed to the file
ports = [int(port) for port in next(reader)]
except StopIteration:
raise IOError('No data in file!')
with open('ports.txt', 'wb') as out_file:
# Create a csv writer object
writer = csv.writer(out_file)
# Append your port to the list of ports...
ports.append(self.myPort)
# ...and write the data to the csv file
writer.writerow(ports)
在处理逗号分隔的值时,通常应使用 下面的代码应该是不言自明的
import csv
# By using the with statement, you don't have to worry about closing the file
# for reading/writing. This is taken care of automaticly.
with open('ports.txt') as in_file:
# Create a csv reader object from the file object. This will yield the
# next row every time you call next(reader)
reader = csv.reader(in_file)
# Put the next(reader) statement inside a try ... except block. If the
# exception StopIteratorion is raised, there is no data in the file, and
# an IOError is raised.
try:
# Use list comprehension to convert all strings to integers. This
# will make sure no leading/trailing whitespace or any newline
# character is printed to the file
ports = [int(port) for port in next(reader)]
except StopIteration:
raise IOError('No data in file!')
with open('ports.txt', 'wb') as out_file:
# Create a csv writer object
writer = csv.writer(out_file)
# Append your port to the list of ports...
ports.append(self.myPort)
# ...and write the data to the csv file
writer.writerow(ports)
在处理逗号分隔的值时,通常应使用 下面的代码应该是不言自明的
import csv
# By using the with statement, you don't have to worry about closing the file
# for reading/writing. This is taken care of automaticly.
with open('ports.txt') as in_file:
# Create a csv reader object from the file object. This will yield the
# next row every time you call next(reader)
reader = csv.reader(in_file)
# Put the next(reader) statement inside a try ... except block. If the
# exception StopIteratorion is raised, there is no data in the file, and
# an IOError is raised.
try:
# Use list comprehension to convert all strings to integers. This
# will make sure no leading/trailing whitespace or any newline
# character is printed to the file
ports = [int(port) for port in next(reader)]
except StopIteration:
raise IOError('No data in file!')
with open('ports.txt', 'wb') as out_file:
# Create a csv writer object
writer = csv.writer(out_file)
# Append your port to the list of ports...
ports.append(self.myPort)
# ...and write the data to the csv file
writer.writerow(ports)
谢谢你的慷慨评论。我很抱歉问这个问题,但是“ports=[int(I)for line in inf for I in line.split(',')]”是什么意思?@eddard.stark:对于ports.txt文件中的每一行,它会将逗号分隔的数字拆分出来,并将每一行转换为整数。然后返回列表中的所有值。最好使用csv模块的方法提取值,而不是重新设计轮子。谢谢您的慷慨评论。我很抱歉问这个问题,但是“ports=[int(I)for line in inf for I in line.split(',')]”是什么意思?@eddard.stark:对于ports.txt文件中的每一行,它会将逗号分隔的数字拆分出来,并将每一行转换为整数。然后返回列表中的所有值。最好使用csv模块的方法提取值,而不是重新设计轮子。谢谢您的慷慨评论。我很抱歉问这个问题,但是“ports=[int(I)for line in inf for I in line.split(',')]”是什么意思?@eddard.stark:对于ports.txt文件中的每一行,它会将逗号分隔的数字拆分出来,并将每一行转换为整数。然后返回列表中的所有值。最好使用csv模块的方法提取值,而不是重新设计轮子。谢谢您的慷慨评论。我很抱歉问这个问题,但是“ports=[int(I)for line in inf for I in line.split(',')]”是什么意思?@eddard.stark:对于ports.txt文件中的每一行,它会将逗号分隔的数字拆分出来,并将每一行转换为整数。然后返回列表中的所有值。最好使用csv模块的方法提取值,而不是重新发明轮子