我是网站和编程的新手,所以如果我的问题最终要回答非常明显,我会提前道歉。我正在使用notepad ++和python 3.2。
所以我的问题是为什么现在在运行和测试我的程序时在python控制台中输入的每一个输入的末尾都添加了'\ r'和/或'\ n'?我已经在互联网上搜索了几天的答案,而我最接近问题的原因是它可能是python将返回键识别为要添加到字符串输入中的字符。我发现这可能是我在所谓的“文本模式”编码。
我尝试用不同的格式编写程序,但没有运气。我的下一个想法是在不同的机器上尝试它并且它应该工作,输入变量中的字符串不包含'\ r'或'\ n'。在此之后,我决定卸载并重新安装python和notepad ++(以及任何剩余的文件)以查看问题是否存在。但同样,它没有任何区别。
这是我的代码。它是一个基本的聊天程序,它读取,写入和保存到一个csv文件,直到最近我才开始遇到这个问题(当处理另一个程序时)。
def get_info():
#get name and a starting message from the user
name = input("what is your name? >")
note = input("what say you? >")
output = ">" + name + ">" + note + "\n"
return(output)
def save_file(the_message):
#this function appends the new message to the group list
#needs argument to write (the_message is the argument)
#returns nothing
hard_drive = open("C:\\Users\Harry\Documents\Homework\A Levels\Computing\chat.csv", "r")
hard_drive.write(the_message)
hard_drive.close()
def read_file():
message_file = open("C:\\Users\Harry\Documents\Homework\A Levels\Computing\chat.csv", "r")
#"r" means read
printable = message_file.read()
message_file.close()
return(printable)
while True:
#calls get_info function in
print_message = get_info()
save_file(print_message)
#saves return value of get_info
messages = read_file()
#calls in read_file and creates variable in which return value is copied
print(messages)
#return value of read_file is printed
它似乎只出现在一个函数中,并且它在我所做的每个其他程序(工作中)都是一样的。这是我在运行程序后调用函数时在控制台中得到的结果:
>>>getinfo()
what is your name? >random
what say you? >anything
'>random\r>anything\r\n'
(函数的返回是底线 - 希望发布一张图片,但我是一个新用户) 我怀疑这是我操作系统的问题(Windows 8) - 任何人都有任何想法?
编辑:当我注意到问题时我正在处理的程序:
print("Address Book v2.6.9 \n")
print("This program will allow the user the add and search for users addresses \n")
def read_file():
file_handle = open("C:\\Users\Harry\Documents\Homework\A Levels\Computing\Python\dataoct_24_2014.csv")
#file is opened and read from, reference held in variable
read = file_handle.read().splitlines()
#variable 'read' holds reference to method which returns all lines from file as list of lists
all_addresses = []
for address in read:
split_fields = address.split("|")
all_addresses.append(split_fields)
#this should make it easier when reading a writing by making a list
#returns a list of lists containing strings, all separated by commas
file_handle.close
return all_addresses
def index():
#this will determine the index number for new people added to the address book
#read_file()
contact_index = (len(read_file()))
#assigns no. contacts the reference to number of lines in file
return contact_index
def get_add_info(first, last, number):
contact_index = index()
#could make a function that assigns each new address with it's correct index
formated_info = str(contact_index) + "|" + first + "|" + last + "|" + str(number)
#remember to add \n to end when writing to file
return formated_info
#def search_file(user_input)
#def append_to_file(add_person):
while True:
user_option = input("Type 'search' to search for a contact or 'add' to add a new contact to your address book: ")
#other options will be created later
if user_option == "search":
print("(this will work later)")
elif user_option == "add":
print("Please enter new contact details: \n")
first_name_input = input("Enter first name of person: ")
last_name_input = input("Enter last tame of person: ")
phone_number_input = input("Enter phone number of person: ")
get_add_info(first_name_input, last_name_input, phone_number_input)
else:
print("Have you spelt you choice correctly?")
答案 0 :(得分:2)
在Python中,“the (backslash) character encodes difficult-to-type characters into a string”。处理反斜杠字符编码称为转义。
print()
函数使用转义。 return()
函数不使用转义,它会输出原始字符串。
例如:
>>>string = "hello\n"
>>>def test(input):
return(input)
>>>test(string)
'hello\n'
>>>print(string)
hello
主脚本中未出现此问题的原因:
messages
包含get_info
print()
messages
功能
由于您使用print()
上的messages
功能,messages
将使用转义显示。
为什么你首先拥有它们:
"\n"
get_info()
基于Mac OS的计算机遵循"CR"(carriage return aka '\r') convention。这意味着写入csv文件的字符串将包含"\r"
行结尾。
现在为什么get_info()
专门显示/插入行结尾,我不确定;我需要您提供更多信息,但这听起来像是Notepad ++设置的问题。
您可以使用write函数删除额外的转义字符。例如,您可以使用rstrip()
。