this StackOverflow 기사를 읽은 후 Word에서 대시/하이픈 (-) 문자를 Excel로 복사하여 붙여 넣은 CSV 파일과 동일한 문제가 있음을 깨달았습니다. .CSV 파일의 특수 문자 바꾸기
Excel 스프레드 시트에서 읽은 데이터로 내 CSV 파일을 만들고 메모장에서 볼 때 표시되지 않는 Excel에 나타나는 이상한 문자를 발견했습니다. SSIS를 사용하여 CSV 파일을 SQL Server 테이블로 전송할 때 이상한 점도있었습니다. 각각의 ASC 값을 검토 한 후, ASC 150 (Dash) 문자와 ASC 45 (하이픈) 문자를 교체하기로 결정했습니다. 이로 인해 문제가 해결되고 Excel에서도 볼 때 하이픈이 정상적으로 나타납니다.
다른 문자를 대체해야 할 수도 있고 비슷한 문제가 발생하지 않도록 내 CSV 파일을 보호하기 위해 사용할 수있는 일반적인 루틴이 있다면 질문 할 수있었습니다.
이것은 현재 내가 CSV 파일에 쓰려는 모든 값에 대해 수행하고있는 작업입니다. 내 getCharacterString 함수는 ASCII 값과 연관된 ASC 문자를 반환 할 때 VB의 CHR 함수와 비슷합니다. 여기
/// <summary>
/// Locates occurrences of targeted special characters found in the input string and replaces each with a space.
/// </summary>
/// <param name="inputString">The input string.</param>
/// <returns>The updated inputString.</returns>
private string ReplaceSpecialCharacters(string inputString)
{
StringBuilder stringBuilder = new StringBuilder(inputString);
const string doubleQuoteCharacter = "\"";
stringBuilder.Replace("\r\n", " "); // Carriage Return/Line Feed characters replaced with single space
stringBuilder.Replace("\r", " "); // Carriage Return replaced with one space if only \r is found
stringBuilder.Replace("\n", " "); // Likewise, Line Feed with a single space
stringBuilder.Replace(this.columnSeparator, " "); // Tab
stringBuilder.Replace(Character.GetCharacterString(150), Character.GetCharacterString(45)); // Replace Dash with Hypen
stringBuilder.Replace(Character.GetCharacterString(147), doubleQuoteCharacter); // Replace angled left quote, “, with simple double quote, ".
stringBuilder.Replace(Character.GetCharacterString(148), doubleQuoteCharacter); // Replace angled left quote, “, with simple double quote, ".
return stringBuilder.ToString();
}
내가 찾은 변환 기능은 다음과 같습니다 : 나는 모든 문자를 식별하는 일반적인 방법을 마련하기 위해 어떻게해야합니까 무엇
:
// -----------------------------------------------------------------------
// <copyright file="Character.cs" company="Joes bar and grill">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------
namespace JoesBarAndGrill.FinanceIT.HhsSweeper
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// TODO: Update summary.
/// </summary>
public static class Character
{
/// <summary>
/// See http://bytes.com/topic/c-sharp/answers/273734-c-chr-asc-function-equivalents-undocumented-truth.
/// </summary>
/// <param name="asciiValue"></param>
/// <returns></returns>
public static string GetCharacterString(int asciiValue)
{
if ((asciiValue < 0) || (asciiValue > 255))
{
throw new ArgumentOutOfRangeException("asciiValue", asciiValue, "Must be between 0 and 255.");
}
byte[] bytBuffer = new byte[] { (byte)asciiValue };
return Encoding.GetEncoding(1252).GetString(bytBuffer);
}
public static int GetAsciiValue(string character)
{
if (character.Length != 1)
{
throw new ArgumentOutOfRangeException("character", character, "Must be a single character.");
}
char[] chrBuffer = { Convert.ToChar(character) };
byte[] bytBuffer = Encoding.GetEncoding(1252).GetBytes(chrBuffer);
return (int)bytBuffer[0];
}
}
}
가 다시 말하지만, 내 질문은 이것이다 이런 경우 전환 문제가 발생할 수 있습니다. 나는 내가 공통적 인 것들만을 확인했을지도 모른다라고 생각한다. 또한 사람들이 대체 문자를 제안하고 대체 할 대상 문자의 전체 목록을 찾도록 도와주는 데 관심이 있습니다.
관련이 있는지 확실하지 않지만 CSV 파일에서 텍스트 구분 기호를 사용한다고 제안하는 경우 SSIS 2008이 올바르게 처리하지 못하기 때문에 텍스트 한정자를 사용하고 있지 않습니다. a previous question of mine)
SSIS 이전에는 [유니 코드 해머] (http://code.activestate.com/recipes/251871-latin1-to-ascii-the-unicode-hammer/)를 사용하여 "재미있는"모든 문자를 손상 시켰습니다 기본 ascii 값으로 변환합니다. .NET 시대에 가장 잘 처리 할 수있는 방법을 생각하지 못했습니다. – billinkc
인수를 위해 문자열을 여러 번 대체하고 다시 작성하는 문자열 작성기를 사용하는 것이 좋습니다. –
@ 닉, 감사합니다. 문자열 연결 작업에 StringBuilders를 사용하고 SB 객체에 Replace 함수가 있다는 것을 전혀 알지 못했습니다. 감사. 코드가 업데이트되었습니다. – ChadD