如何避免换行编码?

时间:2019-07-24 12:41:17

标签: python http

我正在创建一个循环,以发送从txt文件中获取数据的HTTP请求,但是在变量中却出现了一个换行符,该行不知道如何删除:

private static readonly object SyncObject = new object();

public void Delete(MyFile entity)
{
    if (entity != null)
    {
        lock (SyncObject)
        {
            XDocument xDocument;
            using (StreamReader stream = new StreamReader(MyFileXmlPath))
            {
                xDocument = XDocument.Load(stream);
            }
            xDocument.Descendants("ArrayOfMyFile")
                     .Elements()
                     .Where(e => Convert.ToInt32(e.Attribute("Id").Value) == entity.Id)
                     .Remove();
            xDocument.Save(MyFileXmlPath);
        }
    }
}

public IEnumerable<MyFile> GetAll()
{
    XDocument xDocument = XDocument.Load(MyFileXmlPath);
    List<MyFile> MyFiles = xDocument
        .Root
        .Descendants("MyFile")
        .Select(f => new MyFile
        {
            Id = Convert.ToInt32(f.Attribute("Id").Value),
            FilePath = f.Element("FilePath").Value,
            //... other properties
        })
        .ToList();
    return MyFiles;
}

public void Insert(MyFile entity)
{
    if (entity != null)
    {
        lock (SyncObject)
        {
            XDocument xDocument;
            try
            {
                using (StreamReader stream = new StreamReader(MyFileXmlPath))
                {
                    xDocument = XDocument.Load(stream);
                }
                entity.Id = GetFreeId(xDocument);
                MyFile fileToSave = (MyFile)entity;
                XElement newFile = MapMyFile(fileToSave);
                xDocument.Root.Add(newFile);
                xDocument.Save(MyFileXmlPath);
            }
            catch (Exception exception)
            {
                //...
            }
        }             
    }
}

public void Update(MyFile entity)
{
    if (entity != null)
    {
        lock (SyncObject)
        {
            XDocument xDocument;
            try
            {
                using (StreamReader stream = new StreamReader(MyFileXmlPath))
                {
                    xDocument = XDocument.Load(stream);
                }
                XElement updatedFile = xDocument.Descendants("ArrayOfMyFile")
                         .Elements()
                         .Where(e => Convert.ToInt32(e.Attribute("Id").Value) == entity.Id)
                         .FirstOrDefault();

                if (updatedFile != null)
                {
                    updatedFile.ReplaceWith(MapMyFile(entity));
                }
                xDocument.Save(MyFileXmlPath);
            }
            catch (Exception exception)
            {
                //...
            }
        }
    }
}

第一幅画画了这个:

import requests

file = open("uuids.txt", "r")

for uuid in file:
    url = 'http://ip:port/api/assets/changeAssetTemplates/{}'.format(uuid)
    headers = {
        ...
    }
    print(url)
    response = requests.request("GET", url, headers=headers)
    print(response.text)

一个好的网址。但是第二张图描绘了请求中的错误:

http://ip:port/api/assets/changeAssetTemplates/f4c71dd5-13af-428a-a3a5-4cbaf10d9bd3

从哪里可以看到您从uuid变量获取的数据正在换行(%0A),我想知道如何避免此问题。

顺便说一句uuids.txt的示例:

{"timestamp":1563971402621,"status":404,"error":"Not Found","message":"No message available","path":"/api/assets/changeAssetTemplates/f4c71dd5-13af-428a-a3a5-4cbaf10d9bd3%0A"}

2 个答案:

答案 0 :(得分:1)

只需从uuid中删除换行符即可:

for uuid in file:
    url = 'http://ip:port/api/assets/changeAssetTemplates/{}'.format(uuid.replace('\n',''))
    headers = {
        ...
    }
    print(url)
    response = requests.request("GET", url, headers=headers)
    print(response.text)

如果您在Windows上,请小心,换行符可能是'\r\n'而不是'\n';

答案 1 :(得分:0)

您可以简单地删除uuid中的最少char:

url = 'http://ip:port/api/assets/changeAssetTemplates/{}'.format(uuid[:-1])

另一种解决方案:

url = 'http://ip:port/api/assets/changeAssetTemplates/{}'.format(uuid.rstrip("\n"))

PS:readline方法https://docs.python.org/3.3/tutorial/inputoutput.html的Python文档:

  

f.readline()从文件中读取一行;换行符   (\ n)留在字符串的末尾,仅在末尾省略   如果文件未以换行符结尾,则该行。这使得   返回值明确;如果f.readline()返回一个空字符串,则   已到达文件末尾,而空白行表示为   '\ n',仅包含一个换行符的字符串。